BASH Scriptingcan't figure out what's wrong....

statik213

Golden Member
Oct 31, 2004
1,654
0
0
OK, why doesn't the code below work?

if [ -n $2 ] && [ $2 = "clean" ]
then
......
fi

I want to check to see if a second command line param has been specified and if it equals clean...
When I run the script w/o the second param I get...

/bin/r-gas-gcc.sh: line 20: [: =: unary operator expected



 

Barnaby W. Füi

Elite Member
Aug 14, 2001
12,343
0
0
If $2 is empty, then that evaluates to:

if [ -n ] && [ = "clean" ]

which is invalid. You should generally quote variables when in doubt.

if [ -n "$2" ] && [ "$2" = "clean" ]

if $2 is empty, then the above will evaluate to:

if [ -n "" ] && [ "" = "clean" ]

which is ok.

You can also do the ol' if [ x$foo = x ], but that's uglier. There's some benefit to it but I don't remember what exactly it is.
 

n0cmonkey

Elite Member
Jun 10, 2001
42,936
1
0
Originally posted by: BingBongWongFooey
You can also do the ol' if [ x$foo = x ], but that's uglier. There's some benefit to it but I don't remember what exactly it is.

It's bugging me that I can't remember why this is the best way to do it. There was a thread on a mailing list about this in the past couple months, but I can't remember where. And it's not something that's easily googleable. :p
 

statik213

Golden Member
Oct 31, 2004
1,654
0
0
Originally posted by: BingBongWongFooey
If $2 is empty, then that evaluates to:

if [ -n ] && [ = "clean" ]

which is invalid. You should generally quote variables when in doubt.

if [ -n "$2" ] && [ "$2" = "clean" ]

if $2 is empty, then the above will evaluate to:

if [ -n "" ] && [ "" = "clean" ]

which is ok.

You can also do the ol' if [ x$foo = x ], but that's uglier. There's some benefit to it but I don't remember what exactly it is.


thanks..... well... it doesn't make a lot of sense, but i can get used to it..
i mean, i was explicity checking to see if $2 was null or not, and when that fails why does it proceed evaluating the rest of the &&? or does bash do it right->left?

anywayz, thanks a lot ppl... it did work.. :)
 

Barnaby W. Füi

Elite Member
Aug 14, 2001
12,343
0
0
Originally posted by: statik213

i mean, i was explicity checking to see if $2 was null or not,
Empty you mean? There is no concept of null, just empty strings.


and when that fails why does it proceed evaluating the rest of the &&? or does bash do it right->left?
As far as I can tell, it didn't proceed evaluating the && and onwards. And no it's left to right.
 

statik213

Golden Member
Oct 31, 2004
1,654
0
0
Originally posted by: BingBongWongFooey

As far as I can tell, it didn't proceed evaluating the && and onwards. And no it's left to right.

what do you mean?

if i can do this:

if [ -n $2 ] .... note: no quotes around the $2

why shouldn't this:
if [ -n $2 ] && [ $2 = "clean" ]

be legal? the [ -n $2 ] ensures that $2 is a non-empty string. If that tests fails it should not get to the second part....
you can't say that the variable $2 doesn't exist when evaluating the [ $2 = "clean" ] portion so it becomes syntatically wrong....
I mean the -n does take an operand after all, and in that case the -n and = operators are not consistent....

anywayz i'm a newbie to bash scripting (btw is there a lot of difference in scripting across the various shells?), there's probably some fine print somewhere that puts off all this into perspective and i'm probably all wrong... it's just a lil frustrating....

 

Barnaby W. Füi

Elite Member
Aug 14, 2001
12,343
0
0
If $2 is empty then it disappears. Your

if [ -n $2 ]

becomes

if [ -n ]

regardless of whether or not there's anything after it. (I feel like a broken record here)
 

statik213

Golden Member
Oct 31, 2004
1,654
0
0
Originally posted by: BingBongWongFooey
If $2 is empty then it disappears. Your

if [ -n $2 ]

becomes

if [ -n ]

regardless of whether or not there's anything after it. (I feel like a broken record here)

ok...
 

Barnaby W. Füi

Elite Member
Aug 14, 2001
12,343
0
0
BTW, bourne shells (bash, ksh, dash, ash) are mostly the same until you get into more advanced stuff. csh is a whole different story (and an ugly one at that).
 

statik213

Golden Member
Oct 31, 2004
1,654
0
0
thx.... i'm using bash 'cos i don't know any better.
need to play around more w/ linux, just haven't *had* to, till i took this class....
but, thanks a lot for all the info... appreciate it.