问答详情
源自:5-1 shell多分支case语句

谁来帮我解答一下这段代码有什么问题

#!/bin/bash

read -t 30 -p "please input two number:" number

read -t 30 -p "please input yunsuanfu:" number2

read -t 30 -p "please input number" number3

if [ -n "$number" -a -n "$number2" -a -n "$number3" ]

then

test1=$(echo number | sed 's/[0-9]//g')

test2=$(echo number3 | sed 's/[0-9]//g')

test3=$(echo number2 | sed 's/[+-*/]//g')

if [-z "$test1" -a -z "$test -a -z "$test" ]

then

case "$number2" in

"+")

        echo $(( "$number1" + "$number2" ))

        ;;

"*")

        echo $(( "$number1 * "$number2))

        ;;

"-")

        echo $(( "$number1" - "$number2" ))

        ;;

esac


fi

fi


提问者:kkkbbb 2015-07-20 19:06

个回答

  • 努力终不会白费
    2015-11-02 16:16:25
    已采纳

    错误点:

    1、单词写错:number1不存在,上面写的是number 等等

    2、将运算符替换为空,运算符需要进行转义,否则系统把运算符当成正则表达式了


    #!/bin/bash


    read -t 30 -p "please input two number:" number1

    read -t 30 -p "please input yunsuanfu:" number2

    read -t 30 -p "please input number:" number3


    #判断输入的三个参数是否为非空

    if [ -n "$number1" -a -n "$number2" -a -n "$number3" ]

    then

    test1=$(echo "$number1" | sed 's/[0-9]//g')

    test2=$(echo "$number2" | sed 's/[\+\-\*\/]//g')

    test3=$(echo "$number3" | sed 's/[0-9]//g')

    #判断替换后的三个参数的新value是否为空,为空则表示输入的参数格式正确

    if [ -z "$test1" -a -z "$test2" -a -z "$test3" ]

    then

    case "$number2" in

    "+")

           echo $(($number1 $number2 $number3))

           ;;

    "*")

           echo $(($number1 $number2 $number3))

           ;;

    "-")

           echo $(($number1 $number2 $number3))

    ;;

    esac


    fi

    fi