question-- Bourne (and C) SHELL

Arturo Perez arturo at humming.UUCP
Wed Aug 20 08:23:27 AEST 1986


In article <??.arturo at humming.uucp> arturo at 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 at sun.com (or guy at 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 <harvard!seismo!mnetor!utfyzx!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 <harvard!seismo!rick>

$? 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 <homxb!rcc>]

>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!



More information about the Comp.unix mailing list