bash null conditional

Joachim Backes joachim.backes at rhrk.uni-kl.de
Tue Mar 31 05:15:01 UTC 2009


Craig White wrote:
> I'm in my bash book and looking on web but can't seem to resolve this
> simple problem.
> 
> $ if [ -n "grep A121 myfile.csv" ]; then echo "null"; fi
> null
> 
> $ if [ -n "grep A125 myfile.csv" ]; then echo "null"; fi
> null
> 
> A125 definitely is null when I just run the grep command in the quotes
> but A121 definitely is not null.
> 
> What am I missing on the if/null operator here?
> 
> Craig
> 

I think it's simple:

The if sequence is only testing if the strings "grep A121 myfile.csv" or 
"grep A125 myfile.csv" have a non zero lentgh, and both strings have 
definitively non zero length. No grep cmd is executed.

If you want the grep commands to be executed and check the grep output, 
you must use $(grep ...) or `grep ...`.

-- 

Joachim Backes <joachim.backes at rhrk.uni-kl.de>

-------------- next part --------------
A non-text attachment was scrubbed...
Name: smime.p7s
Type: application/x-pkcs7-signature
Size: 6101 bytes
Desc: S/MIME Cryptographic Signature
Url : http://lists.fedoraproject.org/pipermail/users/attachments/20090331/fff24ed1/attachment-0001.bin 


More information about the users mailing list