#!/bin/sh
#mobile recharge shell
#by zkg 2019-08-15
#手机充值10元,第发送一次短信(输出当前余额),花费1角5分,当余额低于1角5分不能发送短信,提示余额不足,请充值(可以允许用户充值继续发送短信)?
#10元=1000分,1角5分=15分,数值要统一,都为整数

#定义变量
TOTAL=1000
CONSUME=200

function isnum(){
expr $1 + 1 &>/dev/null
if [ $? -ne 0 -a "$1" != "-1" ];then
return 1
fi
return 0
}

function consume(){
read -p "please input your message:" content
read -p "Are you sure send?{y|Y|n|N}:" option
case $option in
y|Y)
echo "send $content successfully!!!"
((TOTAL=TOTAL-CONSUME))
echo "you money haved $TOTAL availed"
;;
n|N)
echo "canceled"
;;
*)
echo "invalid input"
;;
esac
}

function recharge(){
if [ $TOTAL -le $CONSUME ];then
echo "you money haved $TOTAL availed,it is not enough!!!"
read -p "you want to recharge money?{y|Y|n|N}:" option2
case $option2 in
y|Y)
while true
do
read -p "please input recharge money?[INT]:" CHARGE
isnum $CHARGE && break ||{
echo "invalid input"
exit 100
}
done
((TOTAL=TOTAL+CHARGE)) && echo "you have $TOTAL money"
;;
n|N)
echo "canceled"
exit 101
;;
*)
echo "invalid input"
exit 102
;;
esac
fi
}

function main(){
while [ $TOTAL -ge $CONSUME ]
do
consume
recharge
done
}
main