if语句中的运算符'<'小于'没有这样的文件或目录'

wax*_*cal 4 bash shell command-line sh

当然这是一个简单的 - 仍然学习我的方式sh脚本.我有:-

if [ $3 < 480 ]; then
  blah blah command
else
   blah blah command2
fi
Run Code Online (Sandbox Code Playgroud)

3美元是一个传递变量,也是一个整数.但是,运行此脚本时,它会报告: -

line 20: 480: No such file or directory
Run Code Online (Sandbox Code Playgroud)

困惑.

Mu *_*iao 9

请使用[ "$3" -lt 480 ]或将其视为括号内的输入重定向.这就是你得到错误的原因:480: No such file or directory.

要查看可用的替代方案:

  • [ "$3" -lt 480 ] - 数字比较,与所有POSIX shell兼容
  • [ "$3" \< 480 ] - 字符串比较(数字通常是错误的!),与所有POSIX shell兼容
  • [[ $3 < 480 ]] - 字符串比较(对于数字通常是错误的!),仅限bash和ksh
  • (( $3 < 480 )) - 数字比较,仅限bash和ksh
  • (( var < 480 ))- 数字比较,仅限bash和ksh,其中$var是包含数字的变量

查看http://www.gnu.org/software/bash/manual/bashref.html#Bash-Conditional-Expressions了解更多信息.