Linux Shell Scripting Tutorial Series (14) Shell Select Tutorial

This article is the (fourteenth) part of the Linux Shell Scripting Series . For more Linux Shell tutorials, please see: Linux Shell Scripting Series Tutorials

In the previous article: Linux Shell Series Tutorial (13) Shell branch statement case ... esac At the end of the tutorial, we briefly introduced the method of using case ... esac to create a menu. In fact, there is another more A professional statement to create a menu: select statement.

 

Select is used with case to complete many complex menu control options.

Select is different from other flow control. There is no similar statement in programming languages ​​such as C. Today, I will introduce the usage of the Shell Select statement.

 

1. Shell Select Statement Syntax

The syntax of the Select statement in Shell is as follows:

select name   [in   list ]
do
    statements that can use  $name...
done

 

Description: select will first generate the menu options in the list list, and then execute the statements between do...done below. The menu item selected by the user is stored in the $name variable.

 

In addition: the select command uses the PS3 prompt, the default is (#?);

When using Select, you can set the prompt string with PS3='string'.

 

2. Examples of Shell Select Statements

Still the same, learn the usage of Shell select through examples:

#!/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 to the user for the user to select, and then displays the menu item selected by the user.

This is a basic example, which mainly shows you the basic usage of select. Of course, you can also remove break and let the program loop all the time.

 

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 slightly modified the example and added 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

 

In this case, the 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 and expanding these statements, very complex scripts can be written. How, isn't it very powerful, hurry up and try it!

For more Linux Shell tutorials, please see: Linux Shell Scripting Series Tutorials

 

Original: Linux Shell Series Tutorial (14) Shell Select Tutorial

Previous: Linux Shell Scripting Tutorial Series (13) Shell branch statement case ... esac tutorial

Next: Linux Shell Scripting Tutorial Series (15) Introduction to Shell Functions

This article is transferred from: Linux Shell Scripting Tutorial Series (14) Shell Select Tutorial

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=326580595&siteId=291194637