Tyler Kelley
Tyler Kelley

Reputation: 11

Creating a Menu Script

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 

Upvotes: 1

Views: 199

Answers (1)

Jahid
Jahid

Reputation: 22428

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.

Upvotes: 1

Related Questions