* Re: [gentoo-user] 'if echo hello' in .bashrc
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
` (3 subsequent siblings)
4 siblings, 1 reply; 19+ messages in thread
From: Christian @ 2009-05-08 14:38 UTC (permalink / raw
To: gentoo-user
Hi Alan,
Am Freitag, 8. Mai 2009 schrieb Alan McKinnon:
> > 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.
is this really right?
The result of
if echo hello|grep --Acolor=auto l >/dev/null 2>&1; then echo hallo; fi
is nothing. So the if clause is false although I pieped STDERR to /dev/null.
> 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
grep writes to STDERR if an error is occured.
The result of
if echo hello|grep --Acolor=auto l >/dev/null ; then echo hallo; fi
is:
grep: Unbekannte Option »--Acolor=auto«
Aufruf: grep [OPTION]... MUSTER [DATEI]...
»grep --help« gibt Ihnen mehr Informationen.
Best regard
Christian
^ permalink raw reply [flat|nested] 19+ messages in thread
* Re: [gentoo-user] 'if echo hello' in .bashrc
2009-05-08 14:38 ` Christian
@ 2009-05-08 14:43 ` Alan McKinnon
0 siblings, 0 replies; 19+ messages in thread
From: Alan McKinnon @ 2009-05-08 14:43 UTC (permalink / raw
To: gentoo-user
On Friday 08 May 2009 16:38:30 Christian wrote:
> Hi Alan,
>
> Am Freitag, 8. Mai 2009 schrieb Alan McKinnon:
> > > 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.
>
> is this really right?
>
> The result of
>
> if echo hello|grep --Acolor=auto l >/dev/null 2>&1; then echo hallo; fi
>
> is nothing.
Which is equal to ), which in shell terms is true
Yes, it's the opposite to other languages.
Yes, it really should be that way.
The return value of successful process is by convention 0, which therefore is
evaluated as true. Non-zero is false
> So the if clause is false although I pieped STDERR to
> /dev/null.
>
> > 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
>
> grep writes to STDERR if an error is occured.
>
> The result of
>
> if echo hello|grep --Acolor=auto l >/dev/null ; then echo hallo; fi
^
What's this? I didn't type it.
--
alan dot mckinnon at gmail dot com
^ permalink raw reply [flat|nested] 19+ messages in thread
* Re: [gentoo-user] 'if echo hello' in .bashrc
2009-05-08 14:10 ` Alan McKinnon
2009-05-08 14:38 ` Christian
@ 2009-05-08 14:57 ` Eray Aslan
2009-05-08 14:59 ` Etaoin Shrdlu
` (2 subsequent siblings)
4 siblings, 0 replies; 19+ messages in thread
From: Eray Aslan @ 2009-05-08 14:57 UTC (permalink / raw
To: gentoo-user
On 08.05.2009 17:10, Alan McKinnon wrote:
>>> if echo hello|grep --color=auto l >/dev/null 2>&1; then
>>> export GREP_OPTIONS='--color=auto' GREP_COLOR='1;32'
>>> fi
>>>
>>> to ~/.bashrc
>>>
>>> Why does he echo hello, please?
>> 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
No. We do not want any output from echo|grep. We just want the exit
code so that the following export statement gets executed iff grep
returns with no errors.
--
Eray
^ permalink raw reply [flat|nested] 19+ messages in thread
* Re: [gentoo-user] 'if echo hello' in .bashrc
2009-05-08 14:10 ` Alan McKinnon
2009-05-08 14:38 ` Christian
2009-05-08 14:57 ` Eray Aslan
@ 2009-05-08 14:59 ` Etaoin Shrdlu
2009-05-08 14:51 ` Alan McKinnon
2009-05-08 16:22 ` Carlos Hendson
2009-05-08 18:05 ` Mike Kazantsev
4 siblings, 1 reply; 19+ messages in thread
From: Etaoin Shrdlu @ 2009-05-08 14:59 UTC (permalink / raw
To: gentoo-user
On Friday 8 May 2009, 16:10, Alan McKinnon wrote:
> On Friday 08 May 2009 16:01:14 Mike Kazantsev wrote:
> > On Fri, 8 May 2009 14:38:58 +0100
> >
> > Stroller <stroller@stellar.eclipse.co.uk> wrote:
> > > To find the part to which I refer you'll need to scroll down about
> > > halfway through that page to "Colorize grep"; the author advises
> > > adding:
> > >
> > > if echo hello|grep --color=auto l >/dev/null 2>&1; then
> > > export GREP_OPTIONS='--color=auto' GREP_COLOR='1;32'
> > > fi
> > >
> > > to ~/.bashrc
> > >
> > > Why does he echo hello, please?
> >
> > 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
That will output an uncaptured error message if --color is not supported.
^ permalink raw reply [flat|nested] 19+ messages in thread
* Re: [gentoo-user] 'if echo hello' in .bashrc
2009-05-08 14:59 ` Etaoin Shrdlu
@ 2009-05-08 14:51 ` Alan McKinnon
2009-05-08 15:13 ` Etaoin Shrdlu
0 siblings, 1 reply; 19+ messages in thread
From: Alan McKinnon @ 2009-05-08 14:51 UTC (permalink / raw
To: gentoo-user
On Friday 08 May 2009 16:59:19 Etaoin Shrdlu wrote:
> On Friday 8 May 2009, 16:10, Alan McKinnon wrote:
> > On Friday 08 May 2009 16:01:14 Mike Kazantsev wrote:
> > > On Fri, 8 May 2009 14:38:58 +0100
> > >
> > > Stroller <stroller@stellar.eclipse.co.uk> wrote:
> > > > To find the part to which I refer you'll need to scroll down about
> > > > halfway through that page to "Colorize grep"; the author advises
> > > > adding:
> > > >
> > > > if echo hello|grep --color=auto l >/dev/null 2>&1; then
> > > > export GREP_OPTIONS='--color=auto' GREP_COLOR='1;32'
> > > > fi
> > > >
> > > > to ~/.bashrc
> > > >
> > > > Why does he echo hello, please?
> > >
> > > 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
>
> That will output an uncaptured error message if --color is not supported.
which is the desired effect. It causes the if to be false and the grep options
are not enabled (as they are not supported)
--
alan dot mckinnon at gmail dot com
^ permalink raw reply [flat|nested] 19+ messages in thread
* Re: [gentoo-user] 'if echo hello' in .bashrc
2009-05-08 14:51 ` Alan McKinnon
@ 2009-05-08 15:13 ` Etaoin Shrdlu
0 siblings, 0 replies; 19+ messages in thread
From: Etaoin Shrdlu @ 2009-05-08 15:13 UTC (permalink / raw
To: gentoo-user
On Friday 8 May 2009, 16:51, Alan McKinnon wrote:
> > > 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
> >
> > That will output an uncaptured error message if --color is not
> > supported.
>
> which is the desired effect. It causes the if to be false and the grep
> options are not enabled (as they are not supported)
It's not the fact that the error message is left free to show on screen
that makes the exit status true or false.
If the file is sourced from a non-interactive environment, you don't want
anything printed (and probably, in this case, neither if it's an
interactive session). Capturing stderr does not change the exit status
of the pipeline.
^ permalink raw reply [flat|nested] 19+ messages in thread
* Re: [gentoo-user] 'if echo hello' in .bashrc
2009-05-08 14:10 ` Alan McKinnon
` (2 preceding siblings ...)
2009-05-08 14:59 ` Etaoin Shrdlu
@ 2009-05-08 16:22 ` Carlos Hendson
2009-05-08 18:08 ` James Rowe
2009-05-08 18:05 ` Mike Kazantsev
4 siblings, 1 reply; 19+ messages in thread
From: Carlos Hendson @ 2009-05-08 16:22 UTC (permalink / raw
To: gentoo-user
Alan McKinnon wrote:
> On Friday 08 May 2009 16:01:14 Mike Kazantsev wrote:
>> On Fri, 8 May 2009 14:38:58 +0100
>>
>> Stroller <stroller@stellar.eclipse.co.uk> wrote:
>>> To find the part to which I refer you'll need to scroll down about
>>> halfway through that page to "Colorize grep"; the author advises adding:
>>>
>>> if echo hello|grep --color=auto l >/dev/null 2>&1; then
>>> export GREP_OPTIONS='--color=auto' GREP_COLOR='1;32'
>>> fi
>>>
>>> to ~/.bashrc
>>>
>>> Why does he echo hello, please?
>> 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
>
The redirection of output doesn't affect the return code of grep in the
above case.
Grep's return code is determined by matching 'l' against the output of
echo hello.
The desired effect of the above code is to evaluate if the --color
option is supported by grep on the system.
The STDERR and STDOUT redirection is an attempt to not pollute the
systems screen when performing that test.
To illustrate:
1. A system that supports --color
$ if echo hello|grep --color=auto l >/dev/null 2>&1; then echo "Grep
returned : $?"; else echo "Grep returned : $?"; fi
Grep returned : 0
2. A system that doesn't support --color (simulated by supplying
--unspported as an option to grep)
$ if echo hello|grep --unsupported l >/dev/null 2>&1; then echo "Grep
returned : $?"; else echo "Grep returned : $?"; fi
-bash: echo: write error: Broken pipe [1]
Grep returned : 2
3. Just to complete the examples, the result of grep not matching echo's
output but still supporting the --color option. (achieved by search
"hello" for the letter 'z')
$ if echo hello|grep --color z >/dev/null 2>&1; then echo "Grep
returned : $?"; else echo "Grep returned : $?"; fi
Grep returned : 1
Regards,
Carlos
[1] The reason an error message is shown here is because it's bash
that's reporting the broken pipe error. Grep's error message was
redirected to /dev/null, which was:
grep: unrecognized option '--unsupported'
Usage: grep [OPTION]... PATTERN [FILE]...
Try `grep --help' for more information.
So even when the system doesn't support --color, that original code will
pollute the screen with bash's error message.
^ permalink raw reply [flat|nested] 19+ messages in thread
* Re: [gentoo-user] 'if echo hello' in .bashrc
2009-05-08 16:22 ` Carlos Hendson
@ 2009-05-08 18:08 ` James Rowe
0 siblings, 0 replies; 19+ messages in thread
From: James Rowe @ 2009-05-08 18:08 UTC (permalink / raw
To: gentoo-user
[-- Attachment #1: Type: text/plain, Size: 1104 bytes --]
* Carlos Hendson (skyclan@gmx.net) wrote:
> [1] The reason an error message is shown here is because it's bash
> that's reporting the broken pipe error. Grep's error message was
> redirected to /dev/null, which was:
>
> grep: unrecognized option '--unsupported'
> Usage: grep [OPTION]... PATTERN [FILE]...
> Try `grep --help' for more information.
>
> So even when the system doesn't support --color, that original code will
> pollute the screen with bash's error message.
SIGPIPE behaviour depends on the shell, how it was built and its
configuration so won't always receive an error.
The point of this mail however is that there is still a way around it,
just call the commands within a subshell. Compare:
$ (echo hello | grep --colour l >/dev/null 2>&1) && echo colour support
colour support
$ (echo hello | grep --broken_arg l >/dev/null 2>&1) && echo broken_arg support
with the original non-subshell'd version:
$ echo hello | grep --broken_arg l >/dev/null 2>&1 && echo broken_arg support
-bash: echo: write error: Broken pipe
Thanks,
James
[-- Attachment #2: Type: application/pgp-signature, Size: 198 bytes --]
^ permalink raw reply [flat|nested] 19+ messages in thread
* Re: [gentoo-user] 'if echo hello' in .bashrc
2009-05-08 14:10 ` Alan McKinnon
` (3 preceding siblings ...)
2009-05-08 16:22 ` Carlos Hendson
@ 2009-05-08 18:05 ` Mike Kazantsev
4 siblings, 0 replies; 19+ messages in thread
From: Mike Kazantsev @ 2009-05-08 18:05 UTC (permalink / raw
To: gentoo-user
[-- 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 --]
^ permalink raw reply [flat|nested] 19+ messages in thread