计算器的脚本程序哪个美女帅哥分享下呀。。。
#!/bin/bash
read -p "number1: " num1
read -p "number2: " num2
read -p "ope: " ope
if [ -n "$num1" -a -n "$num2" -a -n "$ope" ]
then
test1=$(echo $num1 |sed 's/[0-9]//g')
test2=$(echo $num2 |sed 's/[0-9]//g')
else
echo "enter number is null,try again"
exit 12
fi
if [ -z "$test1" -a -z "$test2" ]
then
echo "Please enter a valid value"
exit 10
fi
if [ "$ope"=='+' ]
then
sum=$(( $num1 + $num2 ))
elif [ "$ope"=='-' ]
then
sum=$(( $num1 - $num2 ))
elif [ "$ope"=='*' ]
then
sum=$(( $num1 * $num2 ))
elif [ "$ope"=='/' ]
then
sum=$(( $num1 / $num2 ))
else
echo "Please enter a valid symbol"
exit 11
fi
echo "$num1 $ope $num2 = $sum"
#!/bin/bashread -p "number1: " num1read -p "number2: " num2read -p "ope: " opeif [ -n "$num1" -a -n "$num2" -a -n "$ope" ] then test1=$(echo $num1 |sed 's/[0-9]//g') test2=$(echo $num2 |sed 's/[0-9]//g') else echo "enter number is null,try again" exit 12fiif [ -z "$test1" -a -z "$test2" ] then echo "Please enter a valid value" exit 10fiif [ "$ope"=='+' ] then sum=$(( $num1 + $num2 ))elif [ "$ope"=='-' ] then sum=$(( $num1 - $num2 ))elif [ "$ope"=='*' ] then sum=$(( $num1 * $num2 ))elif [ "$ope"=='/' ] then sum=$(( $num1 / $num2 ))else echo "Please enter a valid symbol" exit 11fiecho "$num1 $ope $num2 = $sum"
#!/bin/bash
read -p "number1: " num1
read -p "number2: " num2
read -p "ope: " ope
if [ -n "$num1" -a -n "$num2" -a -n "$ope" ]
then
test1=$(echo $num1 |sed 's/[0-9]//g')
test2=$(echo $num2 |sed 's/[0-9]//g')
if [ -z "$test1" -a -z "$test2" ]
then
if [ "$ope"=='+' ]
then
sum=$(( $num1 + $num2 ))
elif [ "$ope"=='-' ]
then
sum=$(( $num1 - $num2 ))
elif [ "$ope"=='*' ]
then
sum=$(( $num1 * $num2 ))
elif [ "$ope"=='/' ]
then
sum=$(( $num1 / $num2 ))
else
echo "Please enter a valid symbol"
exit 10
fi
else
echo "Please enter a valid value"
exit 11
fi
else
echo "enter number is null,try again"
exit 12
fi
echo "$num1 $ope $num2 = $sum"
哇唔,谢谢可爱的银儿
#!/bin/bash read -p "please input a num1:" num1 #用键盘输入数字 read -p "please input a num2:" num2 read -p "please input a operator:" ope #判断是否为空 if [ -z "$num1" -a -z "$num2" -a -z "$ope" ] #-z为空取真,中括号里两边需要空格 then echo "is null" exit 10 fi #判断是否为数值,将num1中数字替换为空,来进行判断 test1=$(echo $num1|sed 's/[0-9]//g') test2=$(echo $num2|sed 's/[0-9]//g') if [ -n "$test1" -a -n "$test2" ] #-n 非空取真 then echo "请输入数值" exit 11 fi if [ '$ope'=='+' -o '$ope'=='-' -o '$ope'=='*' -o '$ope'=='/' ] then sum=$(($num1$ope$num2)) #运算要加双括号 echo "$num1$ope$num2=$sum" else exit 12 fi
自己写的,逻辑清晰一些 没有嵌套