Less than the '<' operator in the if expression as a result of "There is no such file or directory"

I am sure this is a simple question still studying my scripts. I have: -

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

$ 3 is the passed variable, again an integer. However, when this script is run, it reports: -

 line 20: 480: No such file or directory 

Confused

+4
source share
3 answers

Please use [ "$3" -lt 480 ] or it will be considered as input redirection inside brackets. This is why you got the error message: 480: No such file or directory .

To view available alternatives:

  • [ "$3" -lt 480 ] - a numerical comparison compatible with all POSIX shells
  • [ "$3" \< 480 ] - string comparison (usually incorrect for numbers!), Compatible with all POSIX shells
  • [[ $3 < 480 ]] - string comparison (usually incorrect for numbers!), Bash and only ksh
  • (( $3 < 480 )) - numerical comparison, bash and only ksh
  • (( var < 480 )) is a numerical comparison, bash and only ksh, where $var is a variable containing a number

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

+8
source

I think you should use:

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

perhaps in the condition you need to use:

 if [[ $3 -lt 480 ]] 
+1
source

All Articles