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

第三个嵌套if简化成这样,没啥漏洞吧,求检测

#!/bin/bash

read -t 30 -p "num2:" num2

read -t 30 -p "operator:" 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')

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

then

if [ $ope=='+' -o $ope=='-' -o $ope=='*' -o $ope=='/' ]

then

res=$((num1 $ope num2))

else

echo "no operator"

exit 10

fi

else

echo "no numbers"

exit 11

fi

else

echo "no values"

exit 12

fi

提问者:gao634209276 2016-02-05 15:26

个回答

  • Rain小瑞子
    2017-10-12 23:26:33

    挺好的,但是运算符同时输入+-×/应该也会运行吧

  • Killven
    2016-02-14 16:05:35

    写的不错哦.