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 |
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
Are you new to LinuxQuestions.org? Visit the following links:
Site Howto |
Site FAQ |
Sitemap |
Register Now
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
 |
GNU/Linux Basic Guide
This 255-page guide will provide you with the keys to understand the philosophy of free software, teach you how to use and handle it, and give you the tools required to move easily in the world of GNU/Linux. Many users and administrators will be taking their first steps with this GNU/Linux Basic guide and it will show you how to approach and solve the problems you encounter.
Click Here to receive this Complete Guide absolutely free. |
|
 |
10-24-2009, 09:28 AM
|
#1
|
|
Member
Registered: Aug 2009
Location: India
Distribution: Fedora 12
Posts: 35
Rep:
|
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.
|
|
|
|
10-24-2009, 09:39 AM
|
#2
|
|
LQ 5k Club
Registered: Dec 2008
Location: Tamil Nadu, India
Distribution: Debian Squeeze (server), Slackware 13.37 (netbook), Slackware64 14.0 (desktop),
Posts: 8,357
|
Try putting this debug line immediately before the if test
Code:
echo "DEBUG: \$max is '$max', \$num is '$num'"
|
|
|
|
10-24-2009, 09:43 AM
|
#3
|
|
Member
Registered: Aug 2009
Location: India
Distribution: Fedora 12
Posts: 35
Original Poster
Rep:
|
@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'
|
|
|
|
10-24-2009, 09:47 AM
|
#4
|
|
Member
Registered: Aug 2009
Location: India
Distribution: Fedora 12
Posts: 35
Original Poster
Rep:
|
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 09:58 AM.
Reason: Wrong use of code blocks
|
|
|
|
10-24-2009, 10:53 AM
|
#5
|
|
Amigo developer
Registered: Dec 2003
Location: Germany
Distribution: Slackware
Posts: 4,592
|
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 ]]
|
|
|
|
10-24-2009, 10:56 AM
|
#6
|
|
Senior Member
Registered: Jul 2005
Distribution: Slackware
Posts: 2,006
Rep: 
|
or use arithmetic expansion so you can use the right symbol for the operator (( $max < $num ))
|
|
|
|
10-24-2009, 11:15 AM
|
#7
|
|
LQ 5k Club
Registered: Dec 2008
Location: Tamil Nadu, India
Distribution: Debian Squeeze (server), Slackware 13.37 (netbook), Slackware64 14.0 (desktop),
Posts: 8,357
|
Quote:
Originally Posted by mangatmodi
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.
|
|
|
|
10-24-2009, 11:28 AM
|
#8
|
|
LQ 5k Club
Registered: Dec 2008
Location: Tamil Nadu, India
Distribution: Debian Squeeze (server), Slackware 13.37 (netbook), Slackware64 14.0 (desktop),
Posts: 8,357
|
Quote:
Originally Posted by gnashley
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.
|
|
|
|
10-24-2009, 12:18 PM
|
#9
|
|
Amigo developer
Registered: Dec 2003
Location: Germany
Distribution: Slackware
Posts: 4,592
|
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...
|
|
|
|
10-25-2009, 01:36 AM
|
#10
|
|
Member
Registered: Aug 2009
Location: India
Distribution: Fedora 12
Posts: 35
Original Poster
Rep:
|
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
|
|
|
|
| Thread Tools |
Search this Thread |
|
|
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
All times are GMT -5. The time now is 08:26 PM.
|
|
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.
|
Latest Threads
LQ News
|
|