bash null conditional

fred smith fredex at fcshome.stoneham.ma.us
Mon Mar 30 20:44:47 UTC 2009


On Mon, Mar 30, 2009 at 11:27:42AM -0700, 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

if you're doing classwork and required to use the if (and related)
operators, then my response isn't what you want.

But if you just want to know how to get equivalent functioniality, 
you may wish to consider this:

	grep A125 myfile.csv || echo "no match found"

which runs the grep and if not "successful" (i.e., did not find a match)
it runs the echo command.

alternatively:

	grep A125 myfile.csv && echo "found a match"

does the echo if it DID find A125.

Fred
-- 
-------------------------------------------------------------------------------
    Under no circumstances will I ever purchase anything offered to me as
    the result of an unsolicited e-mail message. Nor will I forward chain
    letters, petitions, mass mailings, or virus warnings to large numbers
    of others. This is my contribution to the survival of the online
    community.
 --Roger Ebert, December, 1996
----------------------------- The Boulder Pledge -----------------------------
-------------- next part --------------
A non-text attachment was scrubbed...
Name: not available
Type: application/pgp-signature
Size: 189 bytes
Desc: not available
URL: <http://listman.redhat.com/archives/fedora-list/attachments/20090330/b47f11b9/attachment-0001.sig>


More information about the fedora-list mailing list