1 #!/bin/bash
2
3 ##计算器脚本
4
5 read -p "Please input a number:" num1
6 read -p "Please input a yunsuanfu:" ope
7 read -p "PLease input another number:" num2
8 ##先输入运算符和要运算的数字
9
10 if [ !-z "$num1" -a !-z "$num2" -a -z "$ope" ]
11 then
12 ##判断是否有输入数字和运算符
13 test1=$(echo $num1 | sed 's/[0-9]//g')
14 test2=$(echo $num2 | sed 's/[0-9]//g')
15 ##判断输入的num1和num2是否为数字,运算符是否正确
16 if [ -z "$test1" -a -z "$test2" -a -z "$ope" ]
17 then
18 if [ "$ope" == "+" ];then
19 sum=$(( $num1 + $num2 ))
20 elif [ "$ope" == "-" ];then
21 sum=$(( $num1 - $num2 ))
22 elif [ "$ope" == "*" ];then
23 sum=$(( $num1 * $num2 ))
24 elif [ "$ope" == "/" ];then
25 sum=$(( $num1 / $num2 ))
26 else
27 echo "please input a true ope"
28 exit 10
29 #判断是否是正确的运算符
30 fi
31 else
32 echo "Please input a true num"
33 exit 11
34 fi
35 else
36 echo "qing shu ru nei rong"
37 exit 12
38 fi
39
40 echo "$num1 $ope $num2 = $sum"
test.sh: line 10: [: too many arguments
qing shu ru nei rong
if ["$ope"=="+"];then 这里的应该这样 if [ "$ope" == "+" ] ;then
明白了吗,2个等号旁边要有空格,2个中括号跟双引号的内容之间也要有空格,目前发现这个问题
报错中已经说得非常清楚:line10: [: too many arguments:意思是:第10行参数太多。因为脚本: if [ !-z "$num1" -a !-z "$num2" -a -z "$ope" ]中:!与后面的-z之间都没有空格。
上面代码的错误还有第16行的-a -z "ope" 需要删除,错误原因可能是输入ope的值不在上一层if中?