首页 新闻 会员 周边

linux shell 写出错一个关于if多层嵌套的脚本

0
[已解决问题] 解决于 2016-08-11 11:24
#!/bin/bash

read -p "请输入第一个数字:" -t 30 num1
read -p "请输入第二个数字:" -t 30 num2
read -p "请输入操作符:" -t 30 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')
                #测试是否是数字
                                echo "$test1"
                #测试是否输入值
                                echo "$test2"
                        if [ -z $test1 -a -z $test2];then
                                echo $test1
                                echo $num1

                                if [ $ope == '+' ];then
                                result=$(($num1+$num2))
                                echo "$num1+$num2=$result"



                                elif [ $ope == '-' ];then
                                result=$(($num1-$num2))
                                echo "$num1-$num2=$result"


                                elif [ $ope == '*' ];then
                                result=$(($num1*$num2))
                                echo "$num1*$num2=$result"


                                elif [ $ope == '/' ];then
                                result=$(($num1/$num2))
                                echo "$num1/$num2=$result"

                                fi
                        fi
        else
                echo "没有输入字符!"
        fi



报错内容为: line 18: [: too many arguments
小镇伊人的主页 小镇伊人 | 菜鸟二级 | 园豆:204
提问于:2016-08-11 10:44
< >
分享
最佳答案
0

已经找到问题所在了,不是if嵌套的问题,这是嵌套的正确格式,操作符$ope变量没加双引号,导致没有识别出输入的操作符,经测试变量处理后有效。

小镇伊人 | 菜鸟二级 |园豆:204 | 2016-08-11 11:23
清除回答草稿
   您需要登录以后才能回答,未注册用户请先注册