shell_return_codes

This is part of The Pile, a partial archive of some open source mailing lists and newsgroups.



Subject: Re: [RedHat-List] Determining Execution Success or Failure for Commands
From: Cameron Simpson <cs@zip.com.au>
Date: Fri, 21 Jan 2000 10:45:37 +1100


On Thu, Jan 20, 2000 at 12:23:09PM -0700, SoloCDM wrote:
| 1) Is it possible to effectively determine if a command executed
| successfully or failed?

Sure. Every command has an exit status, which is zero on success and
nonzero on failure (some commands have a well documented assortment of
nonzero exits for different types of failure in the manual entry,
though most simply exit with a 1 for all failures).

The shell stores in in the variable $? when the command completes:

	some command
	echo "exit status was $?"

However, it's cleaner to note that the shell considers commands to be its
expression components, thus:

	if some command
	then  echo "The command worked."
	else  echo "The command failed."
	fi

	some command || echo "The command failed."

	some command && echo "The command succeeded."

The "test" (aka "[") command is constructed around this principle, so
the common:

	if [ some test expression ]
	then
	    blah
	fi

is just a special case - that [ ... ] isn't some special shellism,
you're just running the command "[".

| 2) If so, does it work on every command?

It's supposed to. If it doesn't, file a bug report with the author.
BTW, the "make" command depends very heavily on this behaviour, as the
make is meant to stop if some subcommand fails.

===


the rest of The Pile (a partial mailing list archive)

doom@kzsu.stanford.edu