shell脚本--模式匹配case

case 语法结构(字符串比较)

case 变量 in
模式1)
命令序列1
;;
模式2)
命令序列2
;;
模式3)
命令序列3
;;
*)
无匹配后命令序列
esac

简单的模式匹配

邀请用户输入待删除用户名。

if写法:

询问用户,确定要继续删除吗 yes/no: " y
#!/bin/bash
#1请输入删除的用户名:
read -p "please input a username : " user
#2输出用户ID
id $user &> /dev/null
#4判断用户是否存在
if [ $? -ne 0 ];then
        echo "no such user:  $user"
        exit 1
fi
#3请用户确认是否删除
read -p "are you sure?[y/n]: " action
if [ "$action" = "y" -o "$action" = "Y" ] ;then
        userdel -r $user
        echo "$user is deleted!"
else
        echo "thank you"
fi

case写法

#!/bin/bash
read -p "请输入选择" action
case "$action" in
Y|y|YES|yes)
userdel -r $user
echo "$user is deleted!"
;;
*)
echo "thank you"
;;
esac

你可能感兴趣的:(shell脚本,shell,linux,centos,服务器,运维)