shell “integer expression expected”**1
输入一个成绩值,大于90,得A,大于80得B,其他得C!
脚本如下:
-
#!/bin/bash read score
if ["score" -lt 0 -o "score" -gt 100 ] then echo "iput:"
elif [ "score" -ge 90 ] then echo "A"
elif ["score" -ge 80 ] then echo "B"
else echo "C" fi
提示以下错误:
./score1.sh: line 4: [score: command not found
./score1.sh: line 7: [: score: integer expression expected
./score1.sh: line 10: [score: command not found
改成使用"[[]]",逻辑运算符 "-o" 改成 "||" 即可!
-
#!/bin/bash read score
if [["score" -lt 0 || "score" -gt 100 ]] then echo "iput:"
elif [[ "score" -ge 90 ]] then echo "A"
elif [["score" -ge 80 ]] then echo "B"
else echo "C" fi
编辑后能够达到想要的结果!
-
总结
1 所有字符 与逻辑运算符直接用“空格”分开,不能连到一起。
2 [[]] 运算符只是[]运算符的扩充。里面支持逻辑运算符:|| &&
3 -gt -ge -lt -le -nt -eq比较的是数字