注意:若直接复制的的话脚本内容格式会非常乱解决方法
1.Esc
2.光标到开头Ctrl+v
3.上下键选中所有
4.按=即可恢复正确格式
注:脚本内容cat追加部分:cat与最后一个EOF需顶格,自行手动修改注:此脚本逻辑有部分地方有bug,下一篇文章更完善
#!/bin/bash
sum=30 #初始余额总数
msg=15 #发一条短信需要15分
menu () {
cat << EOF
当前余额为$sum 分,每条短信需要$msg 分
==========================
1.充值
2.发消息
3.退出
==========================
EOF
}
recharge () {
read -p "请输入充值金额:" money
expr $money + 1 &>/dev/null
if [ $? -eq 0 ]
then
((sum=$sum+$money))
echo "充值成功,剩余余额为:$sum"
main
else
ec
echo "请输入要充值的正确的金额(正整数)!"
recharge
fi
}
ec () {
echo " "
}
note () {
read -p "输入要发送的内容:" note
((sum=$sum-$msg))
echo "发送成功,余额剩余:$sum"
}
yno () {
read -p "请问是否继续发送(yes/no):" yno
ec
if [ "$yno" == "yes" ]
then
note
elif [ "$yno" == "no" ]
then
exit 0
else
echo "请输入(yes或者no)"
yno
fi
}
panduan () {
read -p "是否充值(yes/no):" yn
ec
if [ "$yn" == "yes" ]
then
recharge
elif [ "$yn" == "no" ]
then
exit 0
else
echo "请输入(yes或者no)"
panduan
fi
}
news () {
if [ $sum -lt 15 ]
then
echo "余额不足$msg,请充值"
panduan
else
while true
do
if [ $sum -lt 15 ]
then
echo "余额不足$msg,如想发送短信请充值"
panduan
else
ec
read -p "输入要发送的内容:" note
((sum=$sum-$msg))
echo "发送成功,余额剩余:$sum"
ec
yno
if [ $sum -lt $msg ]
then
ec
echo "余额不足,剩余$sum分"
panduan
fi
fi
done
fi
}
main () {
while true
do
menu
ec
read -p "请选择要做的操作:" a
sleep 1
clear
case $a in
1)
recharge
;;
2)
news
;;
3)
exit 0
;;
*)
echo "输入错误,请输入正确的选项"
esac
done
}
main
实现信息发送发邮件下面
#!/bin/bash
sum=30 #初始余额总数
msg=15 #发一条短信需要15分
mails=`cat /etc/mail.rc |wc -l`
menu () {
cat << EOF
当前余额为$sum 分,每条短信需要$msg 分
==========================
1.充值
2.发消息
3.退出
==========================
EOF
}
recharge () {
read -p "请输入充值金额:" money
expr $money + 1 &>/dev/null
if [ $? -eq 0 ];then
((sum=$sum+$money))
echo "充值成功,剩余余额为:$sum"
main
else
ec
echo "请输入要充值的正确的金额(正整数)!"
recharge
fi
}
ec () {
echo " "
}
mail () {
if [ $mails -lt 70 ];then
cat >>/etc/mail.rc<< EOF
set from=xxxxxxx@163.com smtp=smtp.163.com smtp-auth-user=xxxxxxx smtp-auth-password=xxxxxxxxxxxxxx smtp-auth=login
EOF
else
:
fi
}
note () {
mail
read -p "输入要发送的内容:" note
((sum=$sum-$msg))
echo $note >/tmp/youjian.txt
mailx -s "短信" xxxxxxxxxx_idc@126.com </tmp/youjian.txt
echo "发送成功,余额剩余:$sum"
}
yno () {
read -p "请问是否继续发送(yes/no):" yno
ec
if [ "$yno" == "yes" ];then
note
elif [ "$yno" == "no" ];then
exit 0
else
echo "请输入(yes或者no)"
yno
fi
}
panduan () {
read -p "是否充值(yes/no):" yn
ec
if [ "$yn" == "yes" ];then
recharge
elif [ "$yn" == "no" ];then
exit 0
else
echo "请输入(yes或者no)"
panduan
fi
}
news () {
if [ $sum -lt 15 ];then
echo "余额不足$msg,请充值"
panduan
else
while true
do
if [ $sum -lt 15 ];then
echo "余额不足$msg,如想发送短信请充值"
panduan
else
ec
note
ec
yno
if [ $sum -lt $msg ];then
ec
echo "余额不足,剩余$sum分"
panduan
fi
fi
done
fi
}
main () {
while true
do
menu
ec
read -p "请选择要做的操作:" a
sleep 1
clear
case $a in
1)
recharge
;;
2)
news
;;
3)
exit 0
;;
*)
echo "输入错误,请输入正确的选项"
esac
done
}
main