How do I ask "if NOT" in shell?

r.peterson roe at sobmips.UUCP
Wed Oct 25 14:49:38 AEST 1989


>From article <2558 at auspex.auspex.com>, by hitz at auspex.auspex.com (Dave Hitz):
> In article <28381 at shemp.CS.UCLA.EDU> kirkaas at oahu.UUCP (paul kirkaas) writes:
>>How can one say "if not" in a shellscript; that is, execute the body of
>>the "then" only if the argument of "if" returns a non-zero exit status?
>>
>>I would like something like:
>>
>>if ! TESTCOMMAND
>>then
>>	BODY
>>fi
> 
> I often define the following in my shell scripts.

[function deleted]

> Unfortunately, not all shells support functions yet, but for those that
> do this works fine.  You can use this just like you requested:
> 
> 	    if NOT grep foobar /etc/passwd >&-
> 	    then
> 		    BODY
> 	    fi
> 

how about:

TESTCOMMAND
if [ ! $? -eq 0 ]
then
	BODY
fi

ie:

grep foobar /etc/passwd >/dev/null 2>&1
if [ ! $? -eq 0 ]    OR:   if test ! $? -eq 0
then
	BODY
fi
-- 
If the brain were so simple we understood it|Roe Peterson
We would be so simple we couldn't.          |{attcan,mcgill-vision}!sobeco!roe



More information about the Comp.unix.questions mailing list