shell 模式匹配:case

帅比萌擦擦* 提交于 2020-02-11 08:25:49

case 变量 in

模式1)

  命令序列1

  ;;

模式2)

  命令序列2

  ;;

模式3)

  命令序列3

*)

  无匹配后命令序列

easc

[root@localhost ~]# cat postfix.sh 
#!/usr/bin/env bash
#start|stop|restart postfix
case "$1" in
start)
    service postfix start
    echo "start postfix"
    ;;
stop)
    service postfix stop
    echo "stop postfix"
    ;;
status)
    service postfix status
    ;;
*)
    echo "Usage:`basename $0` start|stop|status"
esac
[root@localhost ~]# cat mysql_install.sh 
#!/usr/bin/env bash
#install mysql 
#v1.0 by time.catcher
echo "#############################"
echo -e "\t1 mysql5.5"
echo -e "\t2 mysql5.6"
echo -e "\t3 mysql5.7"
read -p "version[1-3]" version
case "$version" in
1)
    echo "install mysql-5.5"
    ;;
2)
    echo "install mysql-5.6"
    ;;
3)
    echo "install mysql-5.7"
    ;;
*)
    echo "Usage please input number[1-3]"
esac

 

#!/usr/bin/env bash
#delete user
#v1.0 by time catcher

read -p "Please input username:" user
id $user &>/dev/null
if [ $? -nq 0 ];then
        echo "error"
        exit 1
fi

read -p "Are you sure yes/no" Action

case $Action in
yes|YES|y|Y)
        userdel $user
        echo "$user is deleted"
        ;;
*)
        echo "error"
esac

 

#!/usr/bin/env bash
#jump server
#v1.0 by time.catcher

cat <<-EOF
    1.web1
    2.web2
    3.web3
EOF

read -p "please input number: " number

case $number in
1)
    ssh root@192.168.244.130
    ;;
2)
    ssh root@192.168.244.131
    ;;
3)    ssh root@192.168.244.132
    ;;
*)    
    echo    "error"
    ;;
esac

 

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!