首页 > 系统服务 > 详细

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

时间:2015-09-09 19:42:41      阅读:292      评论:0      收藏:0      [点我收藏+]
#!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!"


本文出自 “碧血青天” 博客,请务必保留此出处http://tianzhaogao.blog.51cto.com/4716906/1693164

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

原文:http://tianzhaogao.blog.51cto.com/4716906/1693164

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!