On Fri, 8 May 2009 16:10:20 +0200
Alan McKinnon <alan.mckin...@gmail.com> wrote:

> On Friday 08 May 2009 16:01:14 Mike Kazantsev wrote:
>
> > Some greps (like BSD one) might not support '--color' option, so "echo
> > hello|grep --color=auto l" will return error code, skipping if clause,
> > and won't break grep operation by adding an unsupported option.
> 
> except that STDERR is combined with STDOUT and sent to /dev/null so the 
> script 
> will never get it, the if is always true and the entire check is redundant. 
> Better would be
> 
> if echo hello|grep --color=auto l >/dev/null ; then

As many ppl just pointed out, you mistake "output check" done by "[" or
"test" commands with shell built-in "if" statement operation, which
looks only to a given command exit code, doesn't bothering with any
output.

"if [ $A ]" is equivalent to "if test $A" (where "test" is
"/usr/bin/test"), except that modern shells implement test command as a
built-in.
For the rest of them, there's actually "/usr/bin/\[" symlink, which
should exist on your system as well.
So what happens is "test $A", silently returning it's exit code to "if".

And of course, you can use any command instead of "test".
For example there's also "/bin/true" and "/bin/false", so idiomatical
"if true" and "if false" would actually work as expected ;)

-- 
Mike Kazantsev // fraggod.net

Attachment: signature.asc
Description: PGP signature

Reply via email to