unary operator expected

Chuck Robey chuckr at chuckr.org
Tue May 3 18:23:43 PDT 2005


Clifton Royston wrote:
> On Tue, May 03, 2005 at 05:13:47PM -0600, Chris Burchell wrote:
> 
>>I'm working with a script written for Linux that has the following
>>lines:
>>
>># Check that networking is up.
>>[ ${NETWORKING} = "no" ] && exit 0
> 
> 
>   I don't think it's a Linux/BSD issue.  This line won't work in sh if
> NETWORKING is unset.  Then you get (after parameter expansion)
> 
>   [ = "no" ] && exit 0
> 
> which fails the syntax check.  

That's a very well known mistake you ran into ... you did it wrong.  You
assumed that $(NETWORKING) == NO is the same as $(NETWORKING) = "" 
(empty) and they are not equal.  You need to insure that $NETWORKING) 
eitgher always has a value, or you have to shortcircuit the test with a 
test for the variable's existence and length.

Being honest, if I were doing it, I would use NETWORKING as a macro 
value, something that could immediately test the network's viability, 
then operate upon that's results.

> 
>   I suspect "NETWORKING" always happened to be set in the Linux
> environment you were running it under, or perhaps you were using a
> different shell.
>  
> 
>>Can anyone help with suggestions or an alternate statement that will
>>work on FreeBSD 5.3-RELEASE?
> 
> 
>   One time-honored idiom is:
> 
>   [ "X${NETWORKING}" = "Xno" ] && exit 0
> 
>   or you can just make sure that NETWORKING always gets set to some
> value.
> 
>   -- Clifton
> 



More information about the freebsd-questions mailing list