Philip Lee
Philip Lee

Reputation: 21

Continue if response is 'yes'

I have a script which checks for the existence of a directory and runs the following:

 if [ -d /home/philip_lee/csv_dump/$TABLENAME ]; then
     echo "Directory '$TABLENAME' already exists. Delete the existing directory?"
     select yn in "Yes" "No"; do
         case $yn in
             Yes )    rm -rf /home/philip_lee/csv_dump/$TABLENAME
                      break;;
             No )    echo "$EXIT"
                     exit 1;;
         esac
     done
 else
 echo "Please enter the email you would like the results to be sent to:"
 read RECIPIENT
 mkdir /home/philip_lee/csv_dump/$TABLENAME

Following this, there are several more lines of code which populates the directory with csv files. The problem I'm having is that I want the script to proceed to the mkdir command if they select "yes".

So far, the break command provides a partial fix - if "yes" is selected twice in succession, then it deletes the directory and then proceeds to recreate it. This is what currently happens when I try to run the script:

+ '[' -d /home/philip_lee/csv_dump/banks ']'
+ echo 'Directory '\''banks'\'' already exists. Overwrite the existing directory?'
Directory 'banks' already exists. Overwrite the existing directory?
+ select yn in '"Yes"' '"No"'
1) Yes
2) No
#? 1
+ case $yn in
+ rm -rf /home/philip_lee/csv_dump/banks
+ break 1
#? 1
+ case $yn in
+ '[' -d /home/philip_lee/csv_dump/banks ']'
+ echo 'Please enter the email you would like the results to be sent to:'
Please enter the email you would like the results to be sent to:
+ read RECIPIENT
[email protected]
+ mkdir /home/philip_lee/csv_dump/banks

I would like a solution where, if "yes" is selected, then it deletes the directory and then proceeds to the line following, but otherwise exits if "no" is selected.

EDIT: Figured out the source of the problem - the break is actually bringing me back to another yesno query I ran right before it. I just need to figure out a way to prevent the break from bringing me back to the original yesno - This example should help illustrate the problem.

echo "Proceed?"
select yn in "Yes" "No"; do
        case $yn in
                Yes )   echo "Creating new directory for 'banks'..."
                        if [ -d /home/banks ]; then
                                echo "Delete the existing directory?"
                                select yn in "Yes" "No"; do
                                        case $yn in
                                                Yes)    rm -r /home/banks
                                                        break;;
                                                No )    echo "Exit Status 1"
                                                        exit 1;;
                                        esac
                                done
                        else
                                echo "Proceeding..."
                                mkdir /home/banks
                                echo "Proceeded to next step."
                                exit 1
                        fi;;
                No )    echo "Exit Status 2"
                        exit 1;;
        esac
done

Upvotes: 0

Views: 677

Answers (2)

miken32
miken32

Reputation: 42752

You've got the code that makes the directory inside an else statement. As long as the directory exists, it will never get to that command. Try this (just replacing else with fi.)

if [ -d /home/philip_lee/csv_dump/$TABLENAME ]; then
     echo "Directory '$TABLENAME' already exists. Delete the existing directory?"
     select yn in "Yes" "No"; do
         case $yn in
             Yes )    rm -rf /home/philip_lee/csv_dump/$TABLENAME
                      break;;
             No )    echo "$EXIT"
                     exit 1;;
         esac
     done
 fi
 echo "Please enter the email you would like the results to be sent to:"
 read RECIPIENT
 mkdir /home/philip_lee/csv_dump/$TABLENAME

For the record, this is how I would do the same bit of script:

if [ -d "/home/philip_lee/csv_dump/$TABLENAME" ]; then
    read -n 1 -p "Directory '$TABLENAME' already exists. Delete the existing directory? " YN
    if [ $YN != "Y" -a $YN != "y" ]; then
        echo "$EXIT"
        exit 1
    fi
fi
read -p "Please enter the email you would like the results to be sent to: " RECIPIENT
mkdir "/home/philip_lee/csv_dump/$TABLENAME"

Always quote variables; it's unlikely there will be a space in what I assume is a database table name, but it's good practice anyway. read accepts a prompt as part of the command, and doesn't force you into a control structure like select does. case seems like overkill for two options (and personally, I hate the syntax!)

Upvotes: 1

Philip Lee
Philip Lee

Reputation: 21

Managed to resolve the issue by separating out the yes/no loops in this way:

echo "Proceed?"
select yn in "Yes" "No"; do
        case $yn in
                Yes )   break;;
                No )    echo "Exiting...Status 1"
                        exit 1;;
        esac
done
echo "Creating new directory for 'banks'..."
if [ -d /home/philip_lee/csv_dump/banks ]; then
        echo "Directory 'banks' already exists. Delete the existing directory?"
        select yn in "Yes" "No"; do
                case $yn in
                        Yes)    rm -r /home/philip_lee/csv_dump/banks
                                break;;
                        No )    echo "Exiting...Status 2"
                                exit 1;;
                esac
        done
fi
echo "Proceeding..."
mkdir /home/philip_lee/csv_dump/banks
echo "Proceeded to next step."
exit 1

Looks cleaner now, too - thanks to everybody who responded.

Upvotes: 1

Related Questions