Bash Select (Make Menus) | Linuxize

[ad_1]

In this tutorial, we will cover the basics of the select construct in Bash.

The select construct allows you to generate menus.

Bash select Construct #

The select construct generates a menu from a list of items. It has almost the same syntax as the for
loop:

select ITEM in [LIST]
do
  [COMMANDS]
done

The [LIST] can be a series of strings separated by spaces, a range of numbers, output of a command, an array, and so on. A custom prompt for the select construct can be set using the PS3 environment variable
.

When the select construct is invoked, each item from the list is printed on the screen (standard error), preceded with a number.

If the user enters a number that corresponds to the number of one of the displayed items, then the value of [ITEM] is set to that item. The value of the selected item is stored in the variable REPLY. Otherwise, if the user input is empty, the prompt and the menu list are displayed again.

The select loop will continue to run and prompt for user input until the break
command is executed.

To demonstrate how the select construct works, let’s take a look at the following simple example:

PS3="Enter a number: "

select character in Sheldon Leonard Penny Howard Raj
do
    echo "Selected character: $character"
    echo "Selected number: $REPLY"
done

The script will display a menu consisting of list items with an accompanying number and the PS3 prompt. When the user enters a number, the script will print
the selected character and number:

1) Sheldon
2) Leonard
3) Penny
4) Howard
5) Raj
Enter a number: 3
Selected character: Penny
Selected number: 3
Enter a number:

Bash select Example #

Usually, select is used in combination with case
of if
statements.

Let’s take a look at a more practical example. It is a simple calculator that prompt the user for input and performs basic arithmetic operations like addition, subtraction, multiplication, and division.

PS3="Select the operation: "

select opt in add subtract multiply divide quit; do

  case $opt in
    add)
      read -p "Enter the first number: " n1
      read -p "Enter the second number: " n2
      echo "$n1 + $n2 = $(($n1+$n2))"
      ;;
    subtract)
      read -p "Enter the first number: " n1
      read -p "Enter the second number: " n2
      echo "$n1 - $n2 = $(($n1-$n2))"
      ;;
    multiply)
      read -p "Enter the first number: " n1
      read -p "Enter the second number: " n2
      echo "$n1 * $n2 = $(($n1*$n2))"
      ;;
    divide)
      read -p "Enter the first number: " n1
      read -p "Enter the second number: " n2
      echo "$n1 / $n2 = $(($n1/$n2))"
      ;;
    quit)
      break
      ;;
    *) 
      echo "Invalid option $REPLY"
      ;;
  esac
done

When the script is executed, it displays the menu and the PS3 prompt. The user is prompted to select the operation and then to enter two numbers. Depending on the user’s input, the scrip will print the result. The user will be asked to perform a new operation after each selection until the break command is executed.

1) add
2) subtract
3) multiply
4) divide
5) quit
Select the operation: 1
Enter the first number: 4
Enter the second number: 5
4 + 5 = 9
Select the operation: 2
Enter the first number: 4
Enter the second number: 5
4 - 5 = -1
Select the operation: 9
Invalid option 9
Select the operation: 5

One drawback of this script is that it can work only with integers.

Here is a little more advanced version. We are using the bc tool that supports floating numbers to perform mathematical calculations. Also, the repetitive code is grouped inside a function
.

calculate () {
  read -p "Enter the first number: " n1
  read -p "Enter the second number: " n2
  echo "$n1 $1 $n2 = " $(bc -l <<< "$n1$1$n2")
}

PS3="Select the operation: "

select opt in add subtract multiply divide quit; do

  case $opt in
    add)
      calculate "+";;
    subtract)
      calculate "-";;
    multiply)
      calculate "*";;
    divide)
      calculate "/";;
    quit)
      break;;
    *) 
      echo "Invalid option $REPLY";;
  esac
done
1) add
2) subtract
3) multiply
4) divide
5) quit
Select the operation: 4
Enter the first number: 8
Enter the second number: 9
8 / 9 =  .88888888888888888888
Select the operation: 5   

Conclusion #

The select construct allows you to easily generate menus. It is especially useful when writing shell scripts that require user input.

If you have any questions or feedback, feel free to leave a comment.

[ad_2]

Source link

Related Posts

Leave a Reply

Your email address will not be published. Required fields are marked *

This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.