Download Learning the bash Shell, 3rd Edition

Transcript
statement. If the status is 0, the condition evaluates to
true; if it is anything else, the condition is considered
false. The same is true for each condition attached to an
elif statement (if any).
This enables us to write code of the form:
if command ran successfully
then
normal processing
else
error processing
fi
More specifically, we can now improve on the pushd
function that we saw in the last chapter:
pushd ( )
{
dirname=$1
DIR_STACK="$dirname ${DIR_STACK:-$PWD' '}"
cd ${dirname:?"missing directory name."}
echo $DIR_STACK
}
This function requires a valid directory as its argument.
Let's look at how it handles error conditions: if no
argument is given, the third line of code prints an error
message and exits. This is fine.
However, the function reacts deceptively when an
argument is given that isn't a valid directory. In case you
didn't figure it out when reading the last chapter, here is
what happens: the cd fails, leaving you in the same
317