美文网首页
shell脚本第十章10.5优化后

shell脚本第十章10.5优化后

作者: 优秀j | 来源:发表于2019-05-31 19:31 被阅读0次
    注意:若直接复制的的话脚本内容格式会非常乱解决方法

    1.Esc
    2.光标到开头Ctrl+v
    3.上下键选中所有
    4.按=即可恢复正确格式
    注:脚本内容cat追加部分:cat与最后一个EOF需顶格,自行手动修改

    #!/bin/bash
    sum=30      #初始余额总数
    msg=15      #发一条短信需要15分
    
    menu () {
    cat << EOF
    
    当前余额为$sum 分,每条短信需要$msg 分
    
        ==========================
        1.充值
        2.发消息
        3.退出
        ==========================
    EOF
    }
    
    
    recharge () {
        read -p "请输入充值金额:" money
        sleep 1
        clear
        expr $money + 1 &>/dev/null
        if [ -z $money ];then
            echo "不能为空,请重新输入"
            ec
            recharge
        fi
        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
            news
            note
        elif [ "$yno" == "no" ];then
            sleep 1
            clear
            main
        else
            echo "请输入(yes或者no)"
            yno
        fi
    }
    
    panduan () {
        read -p "是否充值(yes/no):" yn
        ec
        if [ "$yn" == "yes" ];then
            recharge
        elif [ "$yn" == "no" ];then
            #       menu
            main
        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
                        menu
                    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
        sleep 1
        clear
        expr $money + 1 &>/dev/null
        if [ -z $money ];then
            echo "不能为空,请重新输入"
            ec
            recharge
        fi
        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  #注意,/etc/mail.rc默认为69行,若曾经修改过需要手动修改
            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 () {
        read -p "输入要发送的内容:" note
        ((sum=$sum-$msg))
        echo $note >/tmp/youjian.txt
        mailx -s "短信" xxxxxxxxx@126.com </tmp/youjian.txt
        echo "发送成功,余额剩余:$sum"
    }
    
    yno () {
        read -p "请问是否继续发送(yes/no):" yno
        ec
        if [ "$yno" == "yes" ];then
            news
            note
        elif [ "$yno" == "no" ];then
            sleep 1
            clear
            main
        else
            echo "请输入(yes或者no)"
            yno
        fi
    }
    
    panduan () {
        read -p "是否充值(yes/no):" yn
        ec
        if [ "$yn" == "yes" ];then
            recharge
        elif [ "$yn" == "no" ];then
            main
        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
                        menu
                    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
    
    

    相关文章

      网友评论

          本文标题:shell脚本第十章10.5优化后

          本文链接:https://www.haomeiwen.com/subject/xcwgtctx.html