1

I am trying to create a menu script that will execute a command when the option is selected. This is what I have so far.

#!/bin/bash

PS3='Please enter you choice: '
options=("Option 1 - File Directory?" "Option 2 - Run MyScript?" "Option 3 - ?" "4 - Quit")
Select opt in "${options[@]}"
do
       case $opt in
            "Option 1 - File Directory?")
                echo "you chose option 1"
                ;;
            "Option 2 - Run MyScript?")
                echo "you chose option 2"
                ;;
            "Option 3 - ?")
                echo "you chose option 3"
                ;;
            "Quit")
                break
                ;;
            *) echo invalid option;;
         esac
done 
4
  • Take a look at shellcheck.net Commented Jun 14, 2015 at 16:08
  • change Select to select and "Quit" to "4 - Quit" or otherwise Commented Jun 14, 2015 at 16:10
  • I need to get the script to display the current directory if option 1 is selected and also to run a shell script that I created if option 2 is selected. How do I do that and/or what are the commands? Commented Jun 14, 2015 at 16:31
  • echo $PWD to print current directory, path_to_the_script to run a script Commented Jun 14, 2015 at 23:45

1 Answer 1

1

Change Select to select and "Quit" to "4 - Quit" inside case or otherwise.

Your code edited:

PS3='Please enter you choice: '
options=("File Directory?" "Run MyScript?" "?" "Quit")
select opt in "${options[@]}"
do
       case $opt in
            "File Directory?")
                echo "you chose option 1"
                ;;
            "Run MyScript?")
                echo "you chose option 2"
                ;;
            "?")
                echo "you chose option 3"
                ;;
            "Quit")
                break
                ;;
            *) echo invalid option;;
         esac
done 

You can check for errors in your shell script with shellcheck.

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

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.