bash null conditional

Craig White craigwhite at azapple.com
Mon Mar 30 21:19:54 UTC 2009


On Mon, 2009-03-30 at 16:44 -0400, fred smith wrote:
> 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.
----
I'm not doing classwork...a little too old for that I think.

I sort of did need the 'if' because I took the line out of a context
from a bigger shell script that does more than this and reduced it down
to the simplest form first for my trial and error and then for the list.

Thanks though...I sort of had those in mind when I started but the
context gets deeper - thus the 
  if [ grep -c some_expression file -eq 0 ]; then ...
did the job nicely.

Craig




More information about the users mailing list