bash null conditional

Justin Willmert jlist at jdjlab.com
Mon Mar 30 18:33:54 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
>
Have you tried
    $ if [ -n $(grep A121 myfile.csv) ]; then echo "null"; fi
    $ if [ -n $(grep A125 myfile.csv) ]; then echo "null"; fi

The double quotes make a string, but it looks like you which to be 
executing grep which is accomplished by using the $() notation.  You 
could also use a pair of backticks surrounding the command.

Justin Willmert




More information about the fedora-list mailing list