问答详情
源自:4-2 shell多分支if语句例:计算器

这个*有毒

#!/bin/bash

if [ -n $1 -a -n $2 -a -n $3 ]
        then
        test1=$( echo $1|sed 's/$1//g' )
        test2=$( echo $3|sed 's/$1//g' )
        if [ -z $test -a -z $test ]
                then
                if [ $2 == "+" ]
                        then
                        result=$(( $1 + $3 ))
                elif [ $2 == "-" ]
                        then
                        result=$(( $1 $2 $3))

                #elif [ $2 == "*"  ] 这儿不能用*怎么解决
                elif [ $2 == "x" ]
                        then
                        result=$(( $1 * $3 ))
                 elif [ $2 == "/" ]
                        then
                        result=$(( $1 $2 $3))
                else
                        echo "输入正确的运算符"
                        exit 3
                fi
        else
                echo "输入的值要是数值"
                exit 2
        fi
        echo $1 $2 $3 : $result
else
        echo "输入不能为空"
        exit 1
fi


提问者:阿愣丶 2017-11-17 22:29

个回答

  • 家迪的家
    2017-11-28 10:15:16

    改为单引号括起来,,,你可以去查一下单括号,双括号,以及中括号的用法