LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   Binary Operator Expected error in Shell Script (https://www.linuxquestions.org/questions/programming-9/binary-operator-expected-error-in-shell-script-764160/)

mangatmodi 10-24-2009 09:28 AM

Binary Operator Expected error in Shell Script
 
I am comparing numbers in a file by shell script. Following anippet of my code gave me line 22: [: –lt: binary operator expected Error.

Code:

max=`head -1 $1`
for file_name in $*
do
      while read num
      do
  if [ $max –lt $num ]; then ## This is line 22
        max=$num;
  fi
  echo $max > file.tmp
      done < $file_name
 done

Now I'm sure that -lt is a binary operator, then why I got this error. It's worth mentioning that I have got same error during test statements in past also.

Anyone, please debug it, as it has already costed me 4 hrs. I have found some similar threads but didn't get any satisfactory answers.

Waiting for reply....

Thank you.

catkin 10-24-2009 09:39 AM

Try putting this debug line immediately before the if test
Code:

echo "DEBUG: \$max is '$max', \$num is '$num'"

mangatmodi 10-24-2009 09:43 AM

@catkin

I did as you said. Both $ max and $ num showed expected values & here is the output:-

Code:

DEBUG: $max is '2', $num is '2'
sortnum.sh: line 22: [: –lt: binary operator expected
DEBUG: $max is '2', $num is '6'
sortnum.sh: line 22: [: –lt: binary operator expected
DEBUG: $max is '2', $num is '3'
sortnum.sh: line 22: [: –lt: binary operator expected
DEBUG: $max is '2', $num is '7'


mangatmodi 10-24-2009 09:47 AM

Hey,

replacing -lt with -ge didn't gave any error ?. It's strange .
By the way, it doesn't serve my logic. So when I changed -ge back to -lt, everything now runs fine.

It's not the first time it's happening. Why is that so, gives errors and runs smoothly without any change. tale a look on my now working code, and see if there is any difference.

Code:

max=`head -1 $1`
for file_name in $*              ##  $* returns all arguments  ##
do
  while read num            ## Read Line by Line the file  ##
  do
echo "DEBUG: \$max is '$max', \$num is '$num'"
if [ $max -lt $num ]; then
        max=$num;
  fi
  echo $max > file.tmp
      done < $file_name
 done

@catkin
Thanks for your quick reply.
can you or anybody please explain this uncertain behaviour of test command ?
it always consumes my lot of time.

any help is much appreciated.

gnashley 10-24-2009 10:53 AM

Quotes are missing. -lt expects binary values instead of strings. So quote both:[ "$max" -lt "$num" ] or use double brackets if this for bash:
[[ $max -lt $num ]]

tuxdev 10-24-2009 10:56 AM

or use arithmetic expansion so you can use the right symbol for the operator (( $max < $num ))

catkin 10-24-2009 11:15 AM

Quote:

Originally Posted by mangatmodi (Post 3730896)
can you or anybody please explain this uncertain behaviour of test command ?

Skwootinising your OP it looks as if the first character of –lt is an "M-dash" rather than an "N-dash" as in -lt.

catkin 10-24-2009 11:28 AM

Quote:

Originally Posted by gnashley (Post 3730967)
Quotes are missing. -lt expects binary values instead of strings. So quote both:[ "$max" -lt "$num" ] or use double brackets if this for bash:
[[ $max -lt $num ]]

No need to quote, in fact it is arguably better not to quote for numeric comparisons because, if either of the comparands evaluates to whitespace then the resulting error message is more helpful.

[[ <test expression> ]] is always preferred over [ <test expression> ] for reasons explained here.

gnashley 10-24-2009 12:18 PM

I guess I didn't read close enough, I just saw this:
for file_name
and thought we were dealing with strings. And anyway they are strings unless he declares them as otherwise.
I agree that using double brackets is the best way to deal with it if it's bash as it will always do what you expect. Single brackets and simple test statements are harder to get right...

mangatmodi 10-25-2009 01:36 AM

Thank You all guys. It is your great efforts that now I am able to remove the test command errors from my shell scripts.

@catkin
Link given by you is really great


All times are GMT -5. The time now is 12:14 AM.