Magazine

Menus with Select in Linux Bash Script

Posted on the 15 April 2021 by Satish Kumar @satish_kumar86

With the Bash shell, it ispossibleto create simple menus with the help of the built-inselectcommand.

The syntax ofselectis as follows:

PS3=prompting-text 
select VARIABLE in item1 item2 item3 
do 
	commands 
done

The advantage of a menu withselectis 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 includinga,bc,def,ghi, andjkl. The script will execute the command insidedoanddone:

select_01.sh
#!/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

It will gives you output similar to below:The following will be the output after executing the preceding commands:

Output:
1) a2) bc3) def4) ghi5) 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 thecasecommand inside thedoanddonepart of theselectmenu. The syntax will be as follows:

           PS3=prompting text 
           select   VARIABLE in  item1  item2 item3 
           do 
             case VARIABLE in 
             value1 ) command1 ; ; 
             value2 ) command2 ; ; 
           esac 
           done

In 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:

select_02.sh
#!/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:

Output:
1) a2) b3) quitplease  select any one : 1option is aplease  select any one : 2option is bplease  select any one : 3

In the following script, select_03.sh, we use a case statement with numerical options 1234, and an option for an invalid choice:

select_03.sh
#!/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 
done

Let’s test the program:

$ chmod +x select_03.sh
$ ./select_03.sh

The following will be the output after executing the preceding commands:

Output:
1) 12) 23) 34) 4Please enter one of the option : 1"One is selected"Please enter one of the option : 2"Two is selectedPlease enter one of the option : 3"Three is selectedPlease enter one of the option : 4"Four is selectedPlease enter one of the option : 8"not a proper option"Please enter one of the option :

In the case statement, we can add many choices to select the same command. Here is an example of the script select_04.sh as follows:

select_04.sh
#!/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 co comp3 selected" ;; 
all) echo "selected all" 
;; 
none) break ;; 
*) echo "ERROR: Invalid selection, $REPLY." ;; 
esac 
done

Let’s test the program:

$ chmod +x select_04.sh
$ ./select_04.sh

The following will be the output after executing the preceding commands:

Output:
1) comp12) comp23) comp34) all5) nonePlease select one of the above:

The scriptselect_05.shis used to inform the user about calorie information in fruits, as follows:

select_05.sh
#!/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 
done

Let’s test the program:

$ chmod +x select_05.sh
$ ./select_05.sh

The following will be the output after executing the preceding commands:

Output:
1) apple      3) banana      5) pear2) orange     4) peach      6) Quit MenuEnter the number for your fruit choice: 1An apple has 80 calories.Enter the number for your fruit choice: 2An orange has 65 calories.Enter the number for your fruit choice: 3A banana has 100 calories.Enter the number for your fruit choice: 4A peach has 38 calories.Enter the number for your fruit choice: 5A pear has 100 calories.Enter the number for your fruit choice: 6

Back to Featured Articles on Logo Paperblog