Select loop
Mainly used to create menus, the menu items arranged numerically are displayed on the standard output, and the PS3 indicator is displayed, waiting for the user to enter a number from the user input menu list, execute the corresponding command,
User input is saved in the built-in variable REPLY
Select is often used in conjunction with case with a for loop, you can omit the in list, at which point the position variable is used
Select is an infinite loop, you must manually specify an exit condition if you want to exit, typically the exit condition is specified in case
Syntax format:
Select variable in list
Do
Circular Body Command
Done
Select Loop Application Example:
Use Select to create a selection menu, 1, add user, 2, delete user, 3, add Group; 4, delete group; 5, exit
#!/bin/bash
#Author: Wangjun
#Contact qq:183530300
#Version: 1.0
#Create time:2016-08-18 19:29:03
#Description: Select test
ps3= "Please choice:"
Select Choose in "Add User" "Delet user" "Add Group" "Delet Group" "Exit"
Do
Case $choose in
"Add User")
Read-p "Please input a new username:" User
! ID $user &>/dev/null && useradd $user &>/dev/null && echo "$user user add Success" | | echo "$user User exists"
;;
"Delet user")
Read-p "Please input a exists username:" User
ID $user &>/dev/null && userdel-r $user &>/dev/null && echo "$user user delet success" | | echo "$user user doesn ' t exist"
;;
"Add Group")
Read-p "Please input a new groupname:" Group
! (Getent Group | grep "^ $group \>") &>/dev/null && groupadd $group &>/dev/null && echo "$ Group group Add Success "| | echo "$group Group exists"
;;
"Delet Group")
Read-p "Please input a exists groupname:" Group
Getent Group | grep "^ $group \>" &>/dev/null && groupdel $group &>/dev/null && echo "$group group Delet Success "| | echo "$group group doesn ' t exist"
;;
Exit
Exit
;;
Esac
Done
This article is from the "Love Firewall" blog, be sure to keep this source http://183530300.blog.51cto.com/894387/1840670
Shell Script Loop statement--select loop