开发者

Less than operator '<' in if statement results in 'No such file or directory'

开发者 https://www.devze.com 2023-03-29 19:26 出处:网络
Sure this is a simple one - still learning my way around sh scripts. I\'ve got:- if [ $3 < 480 ]; then

Sure this is a simple one - still learning my way around sh scripts. I've got:-

if [ $3 < 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:-

line 20: 480: No such file or directory

Confused.


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.

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 $var is a variable containing a number

check http://www.gnu.org/software/bash/manual/bashref.html#Bash-Conditional-Expressions to know more information.


I think you should use:

if [ $3 -lt 480 ]; then
 blah blah command
else
 blah blah command2
fi


probably in condition you need to use:

if [[ $3 -lt 480 ]]
0

精彩评论

暂无评论...
验证码 换一张
取 消