public inbox for gentoo-user@lists.gentoo.org
 help / color / mirror / Atom feed
From: Mike Kazantsev <mike_kazantsev@fraggod.net>
To: gentoo-user@lists.gentoo.org
Subject: Re: [gentoo-user] 'if echo hello' in .bashrc
Date: Sat, 9 May 2009 00:05:07 +0600	[thread overview]
Message-ID: <20090509000507.489eab62@coercion> (raw)
In-Reply-To: <200905081610.20300.alan.mckinnon@gmail.com>

[-- Attachment #1: Type: text/plain, Size: 1390 bytes --]

On Fri, 8 May 2009 16:10:20 +0200
Alan McKinnon <alan.mckinnon@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 #2: signature.asc --]
[-- Type: application/pgp-signature, Size: 198 bytes --]

  parent reply	other threads:[~2009-05-08 18:08 UTC|newest]

Thread overview: 19+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2009-05-08 13:38 [gentoo-user] 'if echo hello' in .bashrc Stroller
2009-05-08 14:01 ` Mike Kazantsev
2009-05-08 14:09   ` Stroller
2009-05-08 14:10   ` Alan McKinnon
2009-05-08 14:38     ` Christian
2009-05-08 14:43       ` Alan McKinnon
2009-05-08 14:57     ` Eray Aslan
2009-05-08 14:59     ` Etaoin Shrdlu
2009-05-08 14:51       ` Alan McKinnon
2009-05-08 15:13         ` Etaoin Shrdlu
2009-05-08 16:22     ` Carlos Hendson
2009-05-08 18:08       ` James Rowe
2009-05-08 18:05     ` Mike Kazantsev [this message]
2009-05-09 10:15 ` Stroller
2009-05-09 10:41   ` Etaoin Shrdlu
2009-05-09 10:43     ` Stroller
2009-05-09 11:13       ` Etaoin Shrdlu
2009-05-09 14:38     ` Joerg Schilling
2009-05-09 11:38   ` Mike Kazantsev

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20090509000507.489eab62@coercion \
    --to=mike_kazantsev@fraggod.net \
    --cc=gentoo-user@lists.gentoo.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox