Linux初学者的简易脚本----计算器

#!bin/bash
#简易计算器
# Author: tianzhaogao (E-mail: 296862380@qq.coom)
 
num1=init
num2=init
cal=init
 
echo "欢迎使用田兆高简易计算器"
read -t 30 -p "num1:" num1
checknum1=$(echo "$num1" | sed 's/[0-9]//g')
 
while [ -z "$num1" -o -n "$checknum1" ]
    do         
        while [ -z "$num1" ]
            do
                echo "Error! The inputing can not be empty!"
                read -t 30 -p "num1:" num1
                checknum1=$(echo "$num1" | sed 's/[0-9]//g')
            done
     
        while [ -n "$checknum1" ]
            do
                echo "Error inputinng! The type of inputing must be int!"
                read -t 30 -p "num1:" num1
                checknum1=$(echo "$num1" | sed 's/[0-9]//g')
            done
    done
 
read -t 30 -p "num2:" num2
checknum2=$(echo "$num2" | sed 's/[0-9]//g')
 
 
while [ -z "$num2" -o -n "$checknum2" ]
    do
        while [ -z "$num2" ]
            do
                echo "Error! The inputing can not be empty!"
                read -t 30 -p "num2:" num2
                checknum2=$(echo "$num2" | sed 's/[0-9]//g')
            done
     
        while [ -n "$checknum2"  ]
            do
                echo "Error inputinng! The type of inputing must be int!"
                read -t 30 -p "num2:" num2
                checknum2=$(echo "$num2" | sed 's/[0-9]//g')
            done
    done
 
read -t 30 -p "运算符:" cal
checkcal=$(echo "$cal" | sed 's/[\+\-\*\/]//g')
 
 
while [ -z "$cal" -o -n "$checkcal" ]
    do
        while [ -z "$cal" ]
            do
                echo "Error! The inputing can not be empty!"
                read -t 30 -p "运算符:" cal
                checkcal=$(echo "$cal" | sed 's/[\+\-\*\/]//g')
            done
     
        while [ -n "$checkcal" ]
            do
                echo "Error inputinng! The type of inputing must be ['+' or '-' or '*' or '/']!"
                read -t 30 -p "运算符:" cal
                checkcal=$(echo "$cal" | sed 's/[\+\-\*\/]//g')
            done
    done
 
if [ "$cal" == "+" ];then
        echo "$num1 $cal $num2 = $(($num1+$num2))"
elif [ "$cal" == "-" ];then
        echo "$num1 $cal $num2 = $(($num1-$num2))"
elif [ "$cal" == "*" ];then
        echo "$num1 $cal $num2 = $(($num1*$num2))"
elif [ "$cal" == "/" ];then
        echo "$num1 $cal $num2 = $(($num1/$num2))"
fi
 
echo "Already completed! Thanks for your using!"

  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值