ProgrammingThis 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.
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.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
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.
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'
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
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 ]]
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.
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...
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.