shell 模式匹配:case

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

 

转载于:https://www.cnblogs.com/weiwenbo/p/6725059.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值