Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
693 views
in Technique[技术] by (71.8m points)

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

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.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...