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

if 怎样改成非嵌套

IF{}

elIF{}

提问者:慕妹2006075 2015-06-15 11:37

个回答

  • CharlesJiang
    2015-10-01 21:29:25
    已采纳

    #!/bin/bash
    echo "Hi, this is a calculater"
    read -t 30 -p "Please input num1:" num1
    read -t 30 -p "Please input num2:" num2
    read -t 30 -p "Please input the operator(+-*/):" ope
    
    if [ -z "$num1" -o -z "$num2" -o -z "$ope" ]
        then
            echo "Sorry, Missed input"
            exit 12
    fi
    
    test1=$(echo $num1 | sed 's/[0-9]//g')
    test2=$(echo $num2 | sed 's/[0-9]//g')
    if [ -n "$test1" -o -n "$test2" ]
        then
            echo "Sorry, Invalid Value"
            exit 11
    fi
    
    if [ "$ope" == "+" ]
        then
            res=$(($num1+$num2))
            echo "+ operated"
    elif [ "$ope" == "-" ]
        then
            res=$(($num1-$num2))
            echo "- operated"
    elif [ "$ope" == "*" ]
        then
            res=$(($num1*$num2))
            echo "* operated"
    elif [ "$ope" == "/" ]
        then
            if [ "$num2" -eq "0" ]
                then
                    echo "Sorry, Can not do this, Bye"
                    exit 10
            else
                res=$(($num1/$num2))
                echo "/ operated"
            fi
    else
        echo "Sorry, Invalid Operator"
        exit 9
    fi
    
    echo "$num1 $ope $num2 : $res"
    echo "Thanks"


  • jql1994
    2016-01-13 21:38:36

     #!/bin/bash
      2 
      3 read -p "Please input your firstnum:"  num1
      4 read -p "Please input your secondnu:"  num2
      5 read -p "please input your operation:" ope
      6 test1=$(echo $num1 | grep "[^0-9]")
      7 test2=$(echo $num2 | grep "[^0-9]")
      8 if [ -z "$num1" -o -z "$num2" -o -z "$ope" ]
      9         then
     10                 echo "Input is NULL"
     11                 exit 10
     12 fi
     13 if [ -n "$test1" -o -n "$test2" ]
     14         then
     15                 echo "Input is not number"
     16                 exit 11
     17 fi
     18 
     19 if [ "$ope" == '+' ]
     20         then
     21          result=$(($num1 + $num2))
     22 elif [ "$ope" == '-' ]
     23         then
     24          result=$(($num1 - $num2))
     25 elif [ "$ope" == '*' ]
     26         then
     27          result=$(($num1 * $num2))
     28 elif [ "$ope" == '/' ]
     29         then
     30          result=$(($num1 / $num2))
     31 else
     32         echo "Ope is error"
     33         exit 12
     34 fi
     35 echo $result


  • jql1994
    2016-01-13 21:37:34

      #我这个方法比老师的简单,判断空不用那么麻烦

     1 #!/bin/bash

      2 

      3 read -p "Please input your firstnum:"  num1

      4 read -p "Please input your secondnu:"  num2

      5 read -p "please input your operation:" ope

      6 test1=$(echo $num1 | grep "[^0-9]")

      7 test2=$(echo $num2 | grep "[^0-9]")

      8 if [ -z "$num1" -o -z "$num2" -o -z "$ope" ]

      9         then

     10                 echo "Input is NULL"

     11                 exit 10

     12 fi

     13 if [ -n "$test1" -o -n "$test2" ]

     14         then

     15                 echo "Input is not number"

     16                 exit 11

     17 fi

     18 

     19 if [ "$ope" == '+' ]

     20         then

     21          result=$(($num1 + $num2))

     22 elif [ "$ope" == '-' ]

     23         then

     24          result=$(($num1 - $num2))

     25 elif [ "$ope" == '*' ]

     26         then

     27          result=$(($num1 * $num2))

     28 elif [ "$ope" == '/' ]

     29         then

     30          result=$(($num1 / $num2))

     31 else

     32         echo "Ope is error"


  • LeeDada
    2015-09-12 17:32:53

    这个实在是太简单了。