Skip site navigation (1)Skip section navigation (2)
Date:      Tue, 20 May 2008 16:01:18 -0700
From:      Chuck Swiger <cswiger@mac.com>
To:        Paul Schmehl <pauls@utdallas.edu>
Cc:        FreeBSD Questions <freebsd-questions@freebsd.org>
Subject:   Re: Shell scripting - suppressing and eliminating error messages
Message-ID:  <4913B976-E1A7-4B46-A63B-392C6B0EA146@mac.com>
In-Reply-To: <83C71638D3A682FC1B026581@utd65257.utdallas.edu>
References:  <83C71638D3A682FC1B026581@utd65257.utdallas.edu>

next in thread | previous in thread | raw e-mail | index | archive | help
Hi, Paul--

On May 20, 2008, at 3:36 PM, Paul Schmehl wrote:
> I'm using the following construction in a pkg-deinstall script for a  
> port I maintain:
>
> if ( ${BATCH} ); then
[ ... ]
> Why is this error printing to stdout and how can I suppress it?  Or  
> is there a flaw in the logic that, if fixed, would resolve this  
> problem?


It's happening because the shell normally tries to run the command in  
the if statement, and evaluate the return value.  Your system doesn't  
have a "1" or "0" command, so that produces the "1: not found"  
message.  For /bin/sh, use the test macro instead:

% BATCH=1
% if [ ${BATCH} -gt 0 ]; then echo yeah; fi
yeah
% BATCH=0
% if [ ${BATCH} -gt 0 ]; then echo yeah; fi

Another way to do this is to use string comparisons, especially if env  
variable is supposed to either be defined or not defined, and what the  
value it is set to if defined doesn't matter:

% unset BATCH
% if [ "x${BATCH}" != "x" ]; then echo yeah; fi
% BATCH=1
% if [ "x${BATCH}" != "x" ]; then echo yeah; fi
yeah
% BATCH=0
% if [ "x${BATCH}" != "x" ]; then echo yeah; fi
yeah

Regards,
-- 
-Chuck




Want to link to this message? Use this URL: <https://mail-archive.FreeBSD.org/cgi/mid.cgi?4913B976-E1A7-4B46-A63B-392C6B0EA146>