Path: utzoo!utgpu!jarvis.csri.toronto.edu!mailrus!wuarchive!cs.utexas.edu!uunet!auspex!hitz From: hitz@auspex.auspex.com (Dave Hitz) Newsgroups: comp.unix.questions Subject: Re: How do I ask "if NOT" in shell? Message-ID: <2558@auspex.auspex.com> Date: 23 Oct 89 22:21:55 GMT References: <28381@shemp.CS.UCLA.EDU> Reply-To: hitz@auspex.auspex.com (Dave Hitz) Organization: Auspex Systems, Santa Clara Lines: 34 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. NOT() { if ${1+"$@"} then return 1; else return 0; fi } 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 Picky people may observe that the arguments to NOT get expanded by the shell twice, but I've never been caught by this. -- Dave Hitz home: 408-739-7116 UUCP: {uunet,mips,sun,bridge2}!auspex!hitz work: 408-970-8970