Dustin Cook
Dustin Cook

Reputation: 1305

Shell Script with If, Select and Case

Can anyone advise how to edit this script to perform the following:

If folder exists - "then are you sure you want to uninstall?" If yes - perform file copying, if not then stop script. Else - can't find folder, stop script.

if [ -e "/tmp/installpackage" ]
then
    echo "Are you sure you want to uninstall?"
    select yn in "Yes" "No"; do
        case $yn in
            Yes ) 
                echo "Beginning uninstall...";
                cp file1.txt original/path/location;
                break;;
            No ) 
                echo "Stopping uninstall.";
                exit 1;;
        esac
    done
else
    echo "Can't find the folder, package not isntalled."
    exit 1
fi

Upvotes: 1

Views: 2111

Answers (4)

hek2mgl
hek2mgl

Reputation: 158280

Your code works as expected. But you have to enter 1 or 2 and not yes or no.

However I would change the first line to:

if [ -d "/tmp/installpackage" ]

-d tests whether the file exists and is a directory

Upvotes: 3

Cole Tierney
Cole Tierney

Reputation: 10324

I would try testing for bail out conditions first then fall through to the uninstall code:

[ ! -d '/tmp/installpackage' ] && \
echo "Can't find the folder, package not isntalled." && exit 1

echo -n 'Are you sure you want to uninstall? [y|n] '
read answer
[[ ! "$answer" = [Yy] ]] && echo 'Stopping uninstall.' && exit 1

echo 'Beginning uninstall...'
cp file1.txt original/path/location

Upvotes: 0

krouis
krouis

Reputation: 129

try

if [ -d "/tmp/installpackage" ]
then
    echo "Are you sure you want to uninstall?"
    select yn in "Yes" "No"; do
        case $REPLY in
            Yes ) 
                echo "Beginning uninstall...";
                cp file1.txt original/path/location;
                break;;
            No ) 
                echo "Stopping uninstall.";
                exit 1;;
            *)
                echo "Incorrect choice";
                break;;
        esac
    done
else
    echo "Can't find the folder, package not installed."
    exit 1
fi

Upvotes: 0

CS Pei
CS Pei

Reputation: 11047

I think after the line esac you want a exit otherwise you have a infinite loop to ask you for answers

Upvotes: 0

Related Questions