bash if 语句中的小于运算符“<”导致“没有这样的文件或目录”
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/7119130/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Less than operator '<' in if statement results in 'No such file or directory'
提问by waxical
Sure this is a simple one - still learning my way around sh scripts. I've got:-
当然这是一个简单的 - 仍在学习我的 sh 脚本。我有:-
if [ < 480 ]; then
blah blah command
else
blah blah command2
fi
$3 is a passed variable, again an integer. However, when this script is run, it reports:-
$3 是一个传递变量,也是一个整数。但是,当此脚本运行时,它会报告:-
line 20: 480: No such file or directory
Confused.
使困惑。
回答by Mu Qiao
Please use [ "$3" -lt 480 ]or it will be treated as input redirection inside the brackets. That's why you got the error: 480: No such file or directory.
请使用[ "$3" -lt 480 ],否则它将被视为括号内的输入重定向。这就是您收到错误的原因:480: No such file or directory.
To review the available alternatives:
要查看可用的替代方案:
[ "$3" -lt 480 ]-- numeric comparison, compatible with all POSIX shells[ "$3" \< 480 ]-- string comparison (generally wrong for numbers!), compatible with all POSIX shells[[ $3 < 480 ]]-- string comparison (generally wrong for numbers!), bash and ksh only(( $3 < 480 ))-- numeric comparison, bash and ksh only(( var < 480 ))-- numeric comparison, bash and ksh only, where$varis a variable containing a number
[ "$3" -lt 480 ]-- 数值比较,兼容所有POSIX shell[ "$3" \< 480 ]-- 字符串比较(数字一般错误!),兼容所有POSIX shell[[ $3 < 480 ]]-- 字符串比较(数字通常是错误的!),仅适用于 bash 和 ksh(( $3 < 480 ))-- 数值比较,仅限 bash 和 ksh(( var < 480 ))-- 数值比较,仅限 bash 和 ksh,其中$var是包含数字的变量
check http://www.gnu.org/software/bash/manual/bashref.html#Bash-Conditional-Expressionsto know more information.
检查http://www.gnu.org/software/bash/manual/bashref.html#Bash-Conditional-Expressions以了解更多信息。
回答by Zitrax
I think you should use:
我认为你应该使用:
if [ -lt 480 ]; then
blah blah command
else
blah blah command2
fi
回答by user478681
probably in condition you need to use:
可能在您需要使用的情况下:
if [[ -lt 480 ]]

