Link to home
Start Free TrialLog in
Avatar of Christian Palacios
Christian PalaciosFlag for Canada

asked on

Bash script - Exit out of choice loop

Hi there,

I have created a bash script that prompts a user to copy data from a USB drive to specified location.  The user is prompted with a list of all of the connected USB drives and they have to choose which one to copy from.  This is working fine, but I can't figure out how to allow the user to exit from the choice menu so exit the script, just in case they don't want to continue.  This is what I have:

####################################################################
select choice in "${HD_DRIVES[@]}"; do

                # Report an error and prompt again.
                [[ -n $choice ]] || { echo "Invalid choice." >&2; continue; }

                # Examine the choice.
                case $choice in
                    *)
                      # Set flag here, or call function, ...
                        COPY_FROM=$choice
                        printf "\n";
                      ;;
                    exit)
                      echo "Exiting. "
                      exit 0
                esac

        break;

done
###################################################################

I took this example from the internet, but if the user is not able to exit out of this loop if they press Ctrl+C or they type 'exit'.  What's an easy way to modify this script so the user can exit out of it?

Thanks in advance!
- Christian
ASKER CERTIFIED SOLUTION
Avatar of woolmilkporc
woolmilkporc
Flag of Germany image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Christian Palacios

ASKER

Perfect, thank you!  That fixed it!

- Christian