Table of contents
- Introduction
- Select statement syntax
- Practical example on select statement used in bash script
- Using the select statement with arrays instead of the direct string
- How to use select statement along with the case statement to execute a block of the statements
Introduction
Select statements are used to build a menu from the input values like array or strings, etc.
It waits for inputs , we can provide the inputs by using the menu item number as prompted and then it assigns that menu item to a variable and executes other lines of codes. It won’t exit after one set of execution as it works like a loop , it waits for another input to execute the same set of codes again assigning new value to the variable . We had to ctrl +c to exit the code.
In this Article we will see how to use the select statements in the bash scripting
Select statement syntax
select item in < strings or array expansion >
do
<other code block>
done
Practical example on select statement used in bash script
In the following script example
Step1 : we feeded the select statement with set of strings to create the menu
Step2 : when we selected one of the menu item , that is assigned to the variable item and used in the further lines of the code
Step3 : it prompts for inputs again
Step4 : to exit we need hit ctrl + c
#script
[root@discoveringsystems select-statement]# cat select-statement-script
#!/bin/bash
echo " Select one of the options available"
select item in apple orange banana grapefruit
do
echo " the selected fruit is $item "
done
#script execution
[root@discoveringsystems select-statement]# ./select-statement-script
Select one of the options available
1) apple
2) orange
3) banana
4) grapefruit
#? 1
the selected fruit is apple
#? 3
the selected fruit is banana
#? ^C
[root@discoveringsystems select-statement]#
Using the select statement with arrays instead of the direct string
In this example , we did the same like before , but this time we were using the arrays in place of set of strings
#script
[root@discoveringsystems select-statement]# cat select-statement-script
#!/bin/bash
echo " Select one of the options available"
array=('apple' 'orange' 'banana' 'grapefruit')
select item in ${array[@]}
do
echo " the selected fruit is $item "
done
#script execution
[root@discoveringsystems select-statement]# ./select-statement-script
Select one of the options available
1) apple
2) orange
3) banana
4) grapefruit
#? 1
the selected fruit is apple
#? 3
the selected fruit is banana
#? ^C
How to use select statement along with the case statement to execute a block of code
We know that it required ctrl + c to exit the select statement used in the scripts , so we incorporated the case statement with an option to help exit the select statement without hitting the ctrl +c , but through other inputs
If you are new to case statement , please go through our other article on how to use case statements for more clarity
How to use case statements in bash scripts
Step1: Same array is used to feed the menu items in the select statement
Step2: User input creates a variable called item due to select statement
Step3 : This variable is used in the case statement to select one option and execute the block of codes
Step4 : one such option is executing the break statement to exit the select statement loop
#script
[root@discoveringsystems select-statement]# cat select-statement-script-2
#!/bin/bash
echo " select the username from the list "
array=('alice' 'bob' 'charlie' 'exit')
select item in ${array[@]}
do
case $item in
Alice | alice)
echo "Welcome Alice ! you role is network admin "
;;
Bob | bob)
echo "Welcome Bob ! your role is network operator "
;;
Charlie | charlie)
echo " Welcome Charlie ! your role is general operator "
;;
exit | Exit)
echo " exiting as per user request "
break
;;
*)
echo " Entered value is incorrect , please re-enter the correct value "
;;
esac
done
#script execution
[root@discoveringsystems select-statement]# ./select-statement-script-2
select the username from the list
1) alice
2) bob
3) charlie
4) exit
#? 2
Welcome Bob ! your role is network operator
#? 4
exiting as per user request