select循环
主要用于创建菜单,按数字顺序排列的菜单项将显示在标准输出上,并显示 PS3示符,等待用户输入用户输入菜单列表中的某个数字,执行相应的命令,
用户输入被保存在内置变量 REPLY 中
select经常和case联合使用与for循环类似,可以省略 in list,此时使用位置变量
select是个无限循环,要想退出必须手动指定退出条件,一般会case中指定退出条件
语法格式:
select variable in list
do
循环体命令
done
select循环应用实例:
使用select创建一个选择菜单,1,添加用户;2,删除用户;3,添加组;4,删除组;5,退出
#!/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