Path: utzoo!attcan!sobmips!roe From: roe@sobmips.UUCP (r.peterson) Newsgroups: comp.unix.questions Subject: Re: How do I ask "if NOT" in shell? Message-ID: <1989Oct25.044938.28972@sobmips.UUCP> Date: 25 Oct 89 04:49:38 GMT References: <2558@auspex.auspex.com> Organization: Sobeco Group - Montreal, Canada Lines: 43 From article <2558@auspex.auspex.com>, by hitz@auspex.auspex.com (Dave Hitz): > In article <28381@shemp.CS.UCLA.EDU> kirkaas@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