1

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
5
  • Did you try with a "read" command? It's easier and more readable. Commented Oct 1, 2013 at 14:57
  • What's the problem with your script? It looks like it does what you want it to. Commented Oct 1, 2013 at 14:59
  • I've tried a few methods at this stage. My script goes through a continuous entry loop and won't actually do anything that I've coded for. Commented Oct 1, 2013 at 15:00
  • Don't you just want break once, before done? Commented Oct 1, 2013 at 15:03
  • This is a modified script, that I found online, to which I've inserted it inside an if statement. Commented Oct 1, 2013 at 15:06

4 Answers 4

3

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

Sign up to request clarification or add additional context in comments.

2 Comments

Actually the expression after case does not require quoting.
@tripleee Yeah this makes sense. (As long as the value list just contains just "yes" and "no"). However, quoting vars is a good practice. Better do it one more than one less - if this won't break anything
0

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

Comments

0

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

Comments

0

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.