question-- Bourne (and C) SHELL

Diane Holt dianeh at ism780c.UUCP
Wed Sep 17 11:36:20 AEST 1986


In article <574 at ih1ap.UUCP> sja at ih1ap.UUCP (Steve Alesch) writes:
>In article <6228 at sun.uucp>, guy at sun.uucp (Guy Harris) 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
>> [or]
>> 	if test ! -f foo
>> 
>Correct me if I'm missing something.  What's wrong with:
>	ls foo
>	if [ $? != 0 ]; then
>		echo foo does not exist
>	fi

I didn't see the original posting, so I'm not sure exactly what's trying to
be done, but my suggestion would be:

	ls foo || echo "foo does not exist"

The || is "OR" -- return "true" from the first command "OR" execute the
next one. Double ampersand (&&) is used to check the return from the first
command "AND" execute the next one. There is no limit on how many "next"
commands there can be or any restrictions on combinations of "OR"s and
"AND"s. I use this all the time.

P.S.
Obviously, this whole example is pretty silly, since 'ls' will output
"foo not found", if it doesn't really exist, so echoing that it doesn't is
superfluous...but I'm assuming that the command used is just an example and
not significant to the original poster's question...if it really *is*
significant, then you'd want to redirect the output: ls foo >/dev/null 2>&1...

Diane Holt
Interactive Systems Corp.
Santa Monica, CA
{seismo,decvax,cbosgd}!hplabs!sdcrdcf!ism780c!dianeh

"But I don't know anything about computers."
"Nobody does...but don't you want one for when you do find out?"



More information about the Comp.unix.wizards mailing list