case语句---是否为空

格式

case 变量引用(要加$) in
PAT1)------>条件1
分支1------>命令1
;;
PAT2)------>条件2
分支2------>命令2
;;
...
*)
默认分支------>最后命令
;;----->可有可无
esac

1.png

实例:选择yes or no(判断是否为空)

read -p "Do you agree: " ANS
if [ "$ANS" = "" ];then
        echo "Please input yes or no!"
else
case "$ANS" in
[Yy]|[Yy][Ee][Ss])
        echo "You do agree"
        ;;
[Nn]|[Nn][Oo])
        echo "You do not agree"
         ;;
*)                                                                                                            
        echo "Your answer is wrong"
esac    
fi

case-yes or no.png

1.png

原文地址:https://www.cnblogs.com/lqynkdcwy/p/9560475.html