Relay-Version: version B 2.10 5/3/83; site utzoo.UUCP Path: utzoo!mnetor!seismo!think!husc6!harvard!humming!arturo From: arturo@humming.UUCP (Arturo Perez) Newsgroups: net.unix Subject: Re: question-- Bourne (and C) SHELL Message-ID: <151@humming.UUCP> Date: Tue, 19-Aug-86 18:23:27 EDT Article-I.D.: humming.151 Posted: Tue Aug 19 18:23:27 1986 Date-Received: Wed, 20-Aug-86 22:20:04 EDT Reply-To: arturo@humming.UUCP (Arturo Perez) Organization: Kurzweil A.I. Waltham, Mass. Lines: 115 Summary: Replies to my question. In article arturo@humming.UUCP writes: >> The key thing here is the ability to NOT the value of status. How is >> this similar thing done in Bourne shell. >> >> if ! ls foo >> then >> echo foo does not exist >> fi > >Try > > if ls foo > then > : > else > echo foo does not exist > fi > >However, you may be better off doing > > if test ! -f foo > > Guy Harris > guy@sun.com (or guy@sun.arpa) [Also suggested by Chris Torek <..!seismo!mimsy.umd.edu!umcp-cs!chris>, harvard!g.cs.cmu.edu!Bennet.Yee, Stu Heiss <..!ihnp4!jpusa1!stu>, and David Harrison ] I guess I confused everyone. What I meant was I wanted a method to directly not the status of a command in an if statement. I don't want to single Guy out but his answer was typical of the response I got (much more nicely worded than some, too). Here are some more: >From seismo!rick From: Rick Adams $? is equivalent to $status. test it the same way you did in csh >From caip!princeton!allegra!ho95e!wcs Look at the man pages for test(1) and sh(1), in particular the sections on standard variables. $? is the return code of the previous command, so you can say foo if [ "$?" != 0 ] then echo "no foo" else echo "foo worked" fi On most Bourne shell versions, and ksh, test is usually a built-in, and "[" is a built-in alias for test. You can't say if [ ! "$?" ] because that means "if "$?" is not empty-string", and $? always has a value. [This is a useful bit of shell lore] From: harvard!caip!princeton!allegra!ulysses!dgk (David Korn) Just use the || operator command || failure_command [Also suggested by Robert C. Chancer ] >From caip!clyde!cbatt!cbdkc1!cbnap!whp How 'bout: ls mojo if [ ! $? ] then echo foo else echo bar fi >From: harvard!violet!seismo!ucb-vax.ARPA!jason Try combinations for test ([) and expr. From topaz!pegasus!hansen csh: ls foo if (! $status) then echo "foo exists" endif sh: ls foo if [ $? != 0 ]; then echo "foo exists" endif If you have the System Vr2 (or later) sh (also available on SUN 3.0) or the ksh, then you can do the following: not() { if eval "$@"; then return 1; else return 0; fi; } and use it exactly as you indicated: if not ls foo then echo foo does not exist fi Tony Hansen ihnp4!pegasus!hansen I think Tony Hansen's answer is the most useful for me. Using his Bourne shell not function I can arbitrarily not the return status of any command in a concise way. This is important because it is SO easy to write non-readable shell scripts. Thank you all! -- "Life is but a dream" - Lope de Vega "...for some and a NIGHTMARE for others!" Merlin, "Excalibur", the movie Disclaimer? What disclaimer? I can back everything up with as much drivel as you like!