From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from pigeon.gentoo.org ([208.92.234.80] helo=lists.gentoo.org) by finch.gentoo.org with esmtp (Exim 4.60) (envelope-from ) id 1M2TmB-0005Do-Ut for garchives@archives.gentoo.org; Fri, 08 May 2009 17:22:24 +0000 Received: from pigeon.gentoo.org (localhost [127.0.0.1]) by pigeon.gentoo.org (Postfix) with SMTP id 31ACEE04FE; Fri, 8 May 2009 17:22:22 +0000 (UTC) Received: from mail.gmx.net (mail.gmx.net [213.165.64.20]) by pigeon.gentoo.org (Postfix) with SMTP id A8718E04FE for ; Fri, 8 May 2009 17:22:21 +0000 (UTC) Received: (qmail invoked by alias); 08 May 2009 17:22:19 -0000 Received: from vol75-13-88-166-24-64.fbx.proxad.net (EHLO [192.168.7.10]) [88.166.24.64] by mail.gmx.net (mp054) with SMTP; 08 May 2009 19:22:19 +0200 X-Authenticated: #5388774 X-Provags-ID: V01U2FsdGVkX1/RSGRU5mLRoI6Miqq3TeyMuyOlD0BJLPPAUlM7oB MYdCSr7zhmi8qH Message-ID: <4A045C2F.90809@gmx.net> Date: Fri, 08 May 2009 18:22:07 +0200 From: Carlos Hendson User-Agent: Thunderbird 2.0.0.21 (X11/20090502) Precedence: bulk List-Post: List-Help: List-Unsubscribe: List-Subscribe: List-Id: Gentoo Linux mail X-BeenThere: gentoo-user@lists.gentoo.org Reply-to: gentoo-user@lists.gentoo.org MIME-Version: 1.0 To: gentoo-user@lists.gentoo.org Subject: Re: [gentoo-user] 'if echo hello' in .bashrc References: <742F0C43-196E-465B-9991-3DFF1C655803@stellar.eclipse.co.uk> <20090508200114.5edbc580@coercion> <200905081610.20300.alan.mckinnon@gmail.com> In-Reply-To: <200905081610.20300.alan.mckinnon@gmail.com> Content-Type: text/plain; charset=iso-8859-15 Content-Transfer-Encoding: 7bit X-Y-GMX-Trusted: 0 X-FuHaFi: 0.52 X-Archives-Salt: 9cca43d7-dae4-4011-bbaf-5cc54ebc6327 X-Archives-Hash: 6a5903f8e4dfa371dc9fa1f36020408a Alan McKinnon wrote: > On Friday 08 May 2009 16:01:14 Mike Kazantsev wrote: >> On Fri, 8 May 2009 14:38:58 +0100 >> >> Stroller 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.