This article is part of the Linux Shell Series Tutorial series, which includes the following 18 parts:
-
- Linux Shell Series Tutorial (a) Shell introduction
-
- Linux Shell Series Tutorial (ii) the first shell script
-
- Linux Shell Series Tutorial (c) Shell variables
-
- Shell comments for the Linux Shell Series Tutorial (d)
-
- Linux Shell Series Tutorial (v) Shell string
-
- Linux Shell Series Tutorial (vi) Shell array
-
- Linux Shell Series Tutorial (vii) Shell output
-
- Linux Shell Series Tutorial (eight) Shell printf command details
-
- Linux Shell Series Tutorial (ix) Shell judging if else usage
-
- Linux Shell Series Tutorial (10) Shell for Loop
-
- Linux Shell Series Tutorial (11) Shell while loop
-
- Linux Shell Series Tutorial (12) Shell until loop
-
- Linux Shell Series Tutorial (13) Shell Branch statement case ... ESAC tutorial
-
- Linux Shell Series Tutorial (14) Shell Select Tutorial
-
- Linux Shell Series Tutorial (15) Introduction to Shell functions
-
- Linux Shell Series Tutorial (16) Shell input and output redirection
-
- The Linux Shell Series Tutorial (17) Shell file contains
-
- Linux Shell Series Tutorials Directory
For more information on the series, see the Linux Shell Series Tutorial:
Linux Shell Series tutorial, Welcome to join the Linux Technology Exchange Group: 479935456
Original: https://www.linuxdaxue.com/linux-shell-select-command.html
Select with case to use, you can complete a lot of complex menu control options.
Select, unlike other flow controls, does not have a similar statement in a programming language such as C, and today introduces the use of the shell SELECT statement.
First, Shell SELECT statement syntax
The syntax for the SELECT statement in the shell is as follows:
select name [in list ]
do
statements that can use $name...
done
Note: Select first produces the menu options in the list, and then executes the statements between the do...done below. The menu item that the user selects is saved in the $name variable.
Additionally: The SELECT command uses the PS3 prompt, which defaults to (#?). ;
In select Use, you can set the hint string with ps3= ' string '.
Ii. Examples of Shell SELECT statements
As usual, learn the use of Shell Select by example:
#! / bin / bash
#Author: linuxdaxue.com
#Date: 2016-05-30
#Desc: Shell select exercise
PS3 = ‘Please choose your number:‘ # Set the prompt string.
echo
select number in "one" "two" "three" "four" "five"
do
echo
echo "Your choose is $ number."
echo
break
done
exit 0
Description: The above example presents a menu for the user to choose from and then displays the menu item selected by the user.
This is one of the most basic examples, showing the basic usage of select. Of course, you can also remove the break and let the program go through the loop.
Here is the output after removing the break:
$./select.sh
1) one
2) two
3) three
4) four
5) five
Please choose your number: 1
Your choose is one.
Please choose your number: 2
Your choose is two.
Please choose your number: 3
Your choose is three.
Please choose your number: 4
Your choose is four.
Please choose your number: 5
Your choose is five.
Then we change the example slightly, adding the CASE...ESAC statement:
#! / bin / bash
#Author: linuxdaxue.com
#Date: 2016-05-30
#Desc: Shell select case exercise
PS3 = ‘Please choose your number:‘ # Set the prompt string.
echo
select number in "one" "two" "three" "four" "five"
do
case $ number in
one)
echo Hello one!
;;
two)
echo Hello two!
;;
*)
echo
echo "Your choose is $ number."
echo
;;
esac
#break
done
exit 0
That way, case will process each of the user's options and then execute the corresponding statement. The output is as follows:
$./select2.sh
1) one
2) two
3) three
4) four
5) five
Please choose your number: 1
Hello one!
Please choose your number: 2
Hello two!
Please choose your number: 3
Your choose is three.
Please choose your number: 4
Your choose is four.
By modifying these statements, you can write very complex scripts. How, is not very strong, quickly try it!
For more Linux shell tutorials see: Linux Shell Series Tutorials
Go Linux Shell Series Tutorial (14) Shell Select Tutorial