Shell scripting - suppressing and eliminating error messages

Chuck Swiger cswiger at mac.com
Tue May 20 23:01:18 UTC 2008


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



More information about the freebsd-questions mailing list