tedwhy 发表于 2018-8-26 10:12:00

Shell的条件表达式介绍

#!/bin/sh  
sum=1000
  
i=500
  
method(){
  
read -p "please input msg:" TXT
  
read -p "send msg,are you sure?" select
  
case "$select" in
  
y)
  
    ((sum=sum-i))
  
    echo "send $TXT is ok!"
  
    echo "you have $sum money!"
  

  
;;
  
n)
  
    echo "you select can't send msg!"
  
    echo "you have $sum money!"
  
    exit 1
  
;;
  
*)
  
echo "USAGE: please input !"
  
exit 2
  
esac
  
}
  
#判断是否为int
  
judge(){
  
    expr $1 + 1 &>/dev/null
  
    #这里有个小问题,如果充值负数也可以...知道的,请赐教...谢谢
  
    if [ $? -ne 0 -a "$1" != "-1" ];then
  
      echo "INVALID INPUT!"
  
      exit 8
  
    else
  
      return 0
  
    fi
  
}
  
while true
  
do
  
if[ $sum -ge $i ];then
  
      method
  
else
  
      read -p "您的余额不足,是否需要充值?" select2
  
      case "$select2" in
  
          y)
  
             read -p "How much do you want to recharge:" add
  
             judge $add
  
             ((sum=sum+${add}))
  
             echo "pay successful! The balance is ${sum} !"
  
             read -p "Whether to continue texting?" msg
  
                case "$msg" in
  
                  y)
  
                        while [ $sum -ge $i ]
  
                        do
  
                        method
  
                        done
  
                        continue
  
                  ;;
  
                  n)
  
                        echo "即将退出!感谢使用!"
  
                        exit 6
  
                  ;;
  
                  *)
  
                        echo "USAGE:您的输入有误!"
  
                        exit 5
  
                esac
  
          ;;
  
          n)
  
             echo "you only $sum money!Can't send msg!"
  
             exit 3
  
         ;;
  
          *)
  
             echo "USAGE: without the select!"
  
             exit 4
  
      esac
  
break
  
fi
  
done


页: [1]
查看完整版本: Shell的条件表达式介绍