您的位置:首页 > 运维架构 > Shell

SHELL练习--手机充值

2016-08-03 14:48 288 查看
[root@db02 shell]# cat phone_recharge01.sh
#!/bin/bash
balance=15
message_cost=15
RED_COLOR='\E[1;31m'
GREEN_COLOR='\E[1;32m'
YELLOW_COLOR='\E[1;33m'
BLUE_COLOR='\E[1;34m'
PINK='\E[1;35m'
RES='\E[0m'
color(){
if [ "$2" = "red" ];then
echo -e "${RED_COLOR}$1 $RES"
elif [ "$2" = "green" ];then
echo -e "${GREEN_COLOR}$1 $RES"
elif [ "$2" = "yellow" ];then
echo -e "${YELLOW_COLOR}$1 $RES"
else
echo "color error."&& exit 1
fi

}
Send_Message(){
echo -e "you have balance \E[1;32m$balance\E[0m,message \E[1;32m$(($balance/${message_cost}))\E[0m left..."
read -p "Input you message:" message
read -p "To whom you want to send:" user
echo -e "\E[1;32mSend:\E[0m $message\n\E[1;32mTo:\E[0m $user"
read -p "Sure?[y|n]:" option
case "$option" in
Y|y)
color "Send successful." green
;;
N|n)
exit 2
;;
*)
color "Input error..." red
Send_Message
;;
esac
}
Recharge(){
read -p "Insufficient balance $balance,recharge right now?[Y|N]:" option
case "$option" in
Y|y)
while true
do
read -p "Input money:" money
expr $money + 1 &>/dev/null
[ $? -eq 0 -a $money -gt 0 ] 2>/dev/null &&break||{
color "Invalid Input,money must be int..." red
}
done
((balance=balance+money))
color "Recharge successful,you balance is $balance .." green
[ $balance -le 15 ]&& Recharge
;;
N|n)
color "bye..." yellow
exit 3
;;
*)
color "Input error..." red
Recharge
;;
esac
}
main(){
while [ $balance -ge 15 ]
do
Send_Message
((balance=balance-${message_cost}))
[ $balance -le 15 ]&& Recharge
done
}
main
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  shell