LinuxQuestions.org
View the Most Wanted LQ Wiki articles.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices

Reply
 
Search this Thread
Old 10-24-2009, 10:28 AM   #1
mangatmodi
Member
 
Registered: Aug 2009
Location: India
Distribution: Fedora 12
Posts: 35

Rep: Reputation: 0
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.
 
Old 10-24-2009, 10:39 AM   #2
catkin
LQ 5k Club
 
Registered: Dec 2008
Location: Tamil Nadu, India
Distribution: Servers: Debian Squeeze and Wheezy. Desktop: Slackware64 14.0. Netbook: Slackware 13.37
Posts: 8,563
Blog Entries: 29

Rep: Reputation: 1179Reputation: 1179Reputation: 1179Reputation: 1179Reputation: 1179Reputation: 1179Reputation: 1179Reputation: 1179Reputation: 1179
Try putting this debug line immediately before the if test
Code:
echo "DEBUG: \$max is '$max', \$num is '$num'"
 
Old 10-24-2009, 10:43 AM   #3
mangatmodi
Member
 
Registered: Aug 2009
Location: India
Distribution: Fedora 12
Posts: 35

Original Poster
Rep: Reputation: 0
@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'
 
Old 10-24-2009, 10:47 AM   #4
mangatmodi
Member
 
Registered: Aug 2009
Location: India
Distribution: Fedora 12
Posts: 35

Original Poster
Rep: Reputation: 0
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.

Last edited by mangatmodi; 10-24-2009 at 10:58 AM. Reason: Wrong use of code blocks
 
Old 10-24-2009, 11:53 AM   #5
gnashley
Amigo developer
 
Registered: Dec 2003
Location: Germany
Distribution: Slackware
Posts: 4,775

Rep: Reputation: 481Reputation: 481Reputation: 481Reputation: 481Reputation: 481
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 ]]
 
Old 10-24-2009, 11:56 AM   #6
tuxdev
Senior Member
 
Registered: Jul 2005
Distribution: Slackware
Posts: 2,014

Rep: Reputation: 115Reputation: 115
or use arithmetic expansion so you can use the right symbol for the operator (( $max < $num ))
 
Old 10-24-2009, 12:15 PM   #7
catkin
LQ 5k Club
 
Registered: Dec 2008
Location: Tamil Nadu, India
Distribution: Servers: Debian Squeeze and Wheezy. Desktop: Slackware64 14.0. Netbook: Slackware 13.37
Posts: 8,563
Blog Entries: 29

Rep: Reputation: 1179Reputation: 1179Reputation: 1179Reputation: 1179Reputation: 1179Reputation: 1179Reputation: 1179Reputation: 1179Reputation: 1179
Quote:
Originally Posted by mangatmodi View Post
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.
 
Old 10-24-2009, 12:28 PM   #8
catkin
LQ 5k Club
 
Registered: Dec 2008
Location: Tamil Nadu, India
Distribution: Servers: Debian Squeeze and Wheezy. Desktop: Slackware64 14.0. Netbook: Slackware 13.37
Posts: 8,563
Blog Entries: 29

Rep: Reputation: 1179Reputation: 1179Reputation: 1179Reputation: 1179Reputation: 1179Reputation: 1179Reputation: 1179Reputation: 1179Reputation: 1179
Quote:
Originally Posted by gnashley View Post
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.
 
Old 10-24-2009, 01:18 PM   #9
gnashley
Amigo developer
 
Registered: Dec 2003
Location: Germany
Distribution: Slackware
Posts: 4,775

Rep: Reputation: 481Reputation: 481Reputation: 481Reputation: 481Reputation: 481
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...
 
Old 10-25-2009, 02:36 AM   #10
mangatmodi
Member
 
Registered: Aug 2009
Location: India
Distribution: Fedora 12
Posts: 35

Original Poster
Rep: Reputation: 0
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
 
  


Reply

Tags
shell scripting


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
error: unary operator expected ?? Lynda_M Programming 3 11-29-2008 09:03 PM
unary operator expected error! Lynda_M Programming 3 11-29-2008 01:04 PM
Binary operator expected - error mike9287 Linux - Newbie 9 07-17-2006 09:27 AM
Unary Operator expected. Bash script Blackout_08 Programming 2 06-22-2006 03:21 PM
shell script provides an error that a binary operator is required max_rsr Linux - General 1 03-12-2005 09:26 AM


All times are GMT -5. The time now is 09:32 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration