select in Usage
The select in loop is used to enhance interactivity. It can display a numbered menu. When the user enters different numbers, he can select different menus and perform different functions.
select in is a unique loop in the shell, which is very suitable for interactive scenarios such as terminals. This is something other languages don't have.
usage:
select variable in value_list #variable represents variable, value_list represents value listdo statements done
1.#? is used to prompt the user to enter the menu number; ^D means pressing the Ctrl+D key combination, its function is to end the select in loop
2. Every time you cycle, selevt will ask the user to enter the menu number and use the value of the environment variable PS3 as a prompt. The default value of PS3 is #?, and modify the value of PS3 to modify the prompt.
3. If the menu number entered by the user is not within the range, then a null value will be assigned to the variable; if the user enters a null value, the menu will be displayed again.
Example 1.
vim #!/bin/bash #test the usage of 'select in'. echo "What is your favourite OS? " select name in "Linux" "Windows" "Mac OS" "Unix" "Android" '//" " can be omitted'do echo $name done ##The select in loop will not automatically jump out of this place, you need to press ctrl+D to jump out of the select inecho "You have selected $name"
run:
[root@server1 ~]# chmod u+x
[root@server1 ~]# ./
What is your favourite OS?
1) Linux
2) Windows
3) Mac OS
4) Unix
5) Android
#? 1
Linux
#? '//Press ctrl+D'
You have selected Linux=======================================================
[root@server1 ~]# ./
What is your favourite OS?
1) Linux
2) Windows
3) Mac OS
4) Unix
5) Android
#? 5
Android
#? 3
Mac OS
#? 2
Windows
#? '//Press ctrl+D'
You have selected Windows
select in usually used with case in
Example 2:
#!/bin/bash #"select in" is usually used with "case in" echo "What is your favourite OS? " select name in "Linux" "Windows" "Mac OS" "Unix" "Android" do case $name in "Linux") echo "Linux is a Unix-like operating system, which is open source and free" break ;; "Windows") echo "Mac OS is a personal computer operating system developed by Microsoft, and it is closed-source charges" break ;; "Mac OS") echo "Mac OS is a graphical interface operating system developed by Apple" break ;; "Unix") echo "Unix is the founder of the operating system and is now only used in some special occasions" break ;; "Android") echo "Android is a mobile operating system developed by Google" break ;; *) echo "Nothing is this option!" break esac done
run:
[root@server1 ~]# ./
What is your favourite OS?
1) Linux
2) Windows
3) Mac OS
4) Unix
5) Android
#? 1
Linux is a Unix-like operating system, which is open source and free[root@server1 ~]# ./
What is your favourite OS?
1) Linux
2) Windows
3) Mac OS
4) Unix
5) Android
#? 2
Mac OS is a personal computer operating system developed by Microsoft, which is closed-source and charged[root@server1 ~]# ./
What is your favourite OS?
1) Linux
2) Windows
3) Mac OS
4) Unix
5) Android
#? 5
Android is a mobile operating system developed by Google
This is the end of this article about the specific use of select in Shell. For more related Shell select in content, please search for my previous articles or continue browsing the related articles below. I hope everyone will support me in the future!