Why does;
if [ "$1" -ge 128 -a -lt 192 ]
Error out with 'too many arguments', where have I gone wrong here? Also I have in the mean time settled with;
if [ "$1" -ge 128 ] && [ $1 -lt 192 ]
Is either one of these preferred over the other?
On 12/09/10 12:25, James Bensley wrote:
Why does;
if [ "$1" -ge 128 -a -lt 192 ]
Error out with 'too many arguments', where have I gone wrong here?
dash says:
[: 6: -lt: unexpected operator
The problem is that -lt wants a integer on the left, and there isn't. So you want to repeat the $1:
if [ "$1" -ge 128 -a "$1" -lt 192 ]
Also I have in the mean time settled with;
if [ "$1" -ge 128 ]&& [ $1 -lt 192 ]
Is either one of these preferred over the other?
That is clearer to my eyes (but be consistent in your quoting of $1).
-- Martijn
On , Martijn Koster mak-alug@greenhills.co.uk wrote:
dash says:
[: 6: -lt: unexpected operator
The problem is that -lt wants a integer on the left, and there isn't.
So you want to repeat the $1:
if [ "$1" -ge 128 -a "$1" -lt 192 ]
Also I have in the mean time settled with;
Thanks Martijn thats seems obvious now!
if [ "$1" -ge 128 ]&& [ $1 -lt 192 ]
Is either one of these preferred over the other?
That is clearer to my eyes (but be consistent in your quoting of $1).
Oh, I missed that, that was I typo because I didn't mean to have quotes there. I assume this is correct since $1 is an integer, do you know if this is the case?
On 12/09/10 14:01, jwbensley@gmail.com wrote:
On , Martijn Koster mak-alug@greenhills.co.uk wrote:
if [ "$1" -ge 128 ]&& [ $1 -lt 192 ]
Is either one of these preferred over the other?
That is clearer to my eyes (but be consistent in your quoting of $1).
Oh, I missed that, that was I typo because I didn't mean to have quotes there. I assume this is correct since $1 is an integer, do you know if this is the case?
If you indeed pass an integer number to the script as an argument, then $1 will contain that, and it will work as you expect. But it's fragile, and not recommended for the general case. To explain (do a "cat s.sh" to see the script):
mak@yoda:~$ echo "if [ $1 -ge 128 ]; then echo yes; else echo no; fi"> s.sh mak@yoda:~$ bash s.sh 1 no mak@yoda:~$ bash s.sh 'a b' s.sh: line 1: [: too many arguments no
Note the "too many arguments" error. That's because bash turns your call into:
if [ a b -ge 128 ]; then echo yes; else echo no; fi
which makes no sense as there are two strings to left of the -ge, making it a syntax error in the expression. It would be more correct to quote properly:
mak@yoda:~$ echo "if [ "$1" -ge 128 ]; then echo yes; else echo no; fi"> s.sh mak@yoda:~$ bash s.sh 'a b' s.sh: line 1: [: a b: integer expression expected no
Now it becomes:
if [ 'a b' -ge 128 ]; then echo yes; else echo no; fi
which only has a single argument to the left of the -ge, and you get a more accurate error.
A more typical example is a script that takes a filename or directory name and does something with it. If you don't quote properly inside your script, it will fail to work correctly on files or directories with spaces in the name.
-- Martijn