With the Bash shell, it is possible to create simple menus with the help of the select built-in command.
The syntax of select is as follows:
PS3=prompting-text
select VARIABLE in item1 item2 item3
do
commands
doneThe advantage of a menu with select is that we can have an endless loop with it. We can have a condition in which we exit the loop.
In the following script select_01.sh, we show the menu with five options such as a, bc, def, ghi, and jkl. The script will execute the command inside do and done:
#!/bin/bash select var1 in a bc def ghi jkl do echo "Present value of var1 is $var1 done
Let's test the program:
$ chmod +x select_01.sh $ ./select_01.sh
The following will be the output after executing the preceding commands:
1) a 2) bc 3) def 4) ghi 5) jkl #? 2 "Present value of var1 is bc #? 4 "Present value of var1 is ghi #? 5 "Present value of var1 is jkl #? Press ^C to quit
We can implement the case command inside the do and done part of the select menu. The syntax will be as follows:
PS3=prompting text
select VARIABLE in item1 item2 item3
do
case VARIABLE in
value1 ) command1 ; ;
value2 ) command2 ; ;
esac
doneIn the following script select_02.sh, we used the case command inside do and done. This gives us many convenient features. Due to select, we get endless such as continuous loop. In case the if option entered is quit, then it exits the continuous loop:
#!/bin/bash PS3="please select any one : " select var in a b quit do case $var in a) echo option is a ;; b) echo option is b ;; quit) exit ;; *) echo option is default ;; esac done
Let's test the program:
$ chmod +x select_02.sh $ ./select_02.sh
The following will be the output after executing the preceding commands:
1) a 2) b 3) quit please select any one : 1 option is a please select any one : 2 option is b please select any one : 3
In the following script select_03.sh, we used a case with numerical options 1, 2, 3, 4, and an option for an invalid choice:
#!/bin/bash
PS3="Please enter one of the option"
select var in 1 2 3 4
do
case $var in
1) echo "One is selected";;
2) echo "Two is selected;;
3) echo "Two is selected;;
4) echo "Two is selected;;
*) echo "not a proper option";;
esac
doneLet's test the program:
$ chmod +x select_03.sh $ ./select_03.sh
The following will be the output after executing the preceding commands:
1) 1 2) 2 3) 3 4) 4 Please enter one of the option : 1 "One is selected" Please enter one of the option : 2 "Two is selected Please enter one of the option : 3 "Two is selected Please enter one of the option : 4 "Two is selected Please enter one of the option : 8 "not a proper option" Please enter one of the option :
In the case statement, we can put many choices to select the same command. Here is an example of the script select_04.sh as follows:
#!/bin/bash PS3="Please select one of the above:" select COMPONENT in comp1 comp2 comp3 all none do case $COMPONENT in comp1|comp2|comp3) echo "comp1 or comp2 or comp3 selected" ;; all) echo "selected all" ;; none) break ;; *) echo "ERROR: Invalid selection, $REPLY." ;; esac done
$ chmod +x select_04.sh $ ./select_04.sh
The following will be the output after executing the preceding commands:
1) comp1 2) comp2 3) comp3 4) all 5) none Please select one of the above:
The script select_05.sh is used to inform the user about the calorie information in fruits as follows:
#!/bin/bash
PS3="Enter the number for your fruit choice: "
select fruit in apple orange banana peach pear "Quit Menu"
do
case $fruit in
apple)
echo "An apple has 80 calories."
;;
orange)
echo "An orange has 65 calories."
;;
banana)
echo "A banana has 100 calories."
;;
peach)
echo "A peach has 38 calories."
;;
pear)
echo "A pear has 100 calories."
;;
"Quit Menu")
break
;;
*)
echo "You did not enter a correct choice."
;;
esac
doneLet's test the program:
$ chmod +x select_05.sh $ ./select_05.sh
The following will be the output after executing the preceding commands:
1) apple 3) banana 5) pear 2) orange 4) peach 6) Quit Menu Enter the number for your fruit choice: 1 An apple has 80 calories. Enter the number for your fruit choice: 2 An orange has 65 calories. Enter the number for your fruit choice: 3 A banana has 100 calories. Enter the number for your fruit choice: 4 A peach has 38 calories. Enter the number for your fruit choice: 5 A pear has 100 calories. Enter the number for your fruit choice: 6