Zsh Mailing List Archive
Messages sorted by:
Reverse Date,
Date,
Thread,
Author
Re: which command failed?
- X-seq: zsh-users 12741
- From: Bart Schaefer <schaefer@xxxxxxxxxxxxxxxx>
- To: zsh-users@xxxxxxxxxx
- Subject: Re: which command failed?
- Date: Fri, 28 Mar 2008 08:04:33 -0700
- In-reply-to: <20080328105545.81550.qmail@xxxxxxxxxxx>
- Mailing-list: contact zsh-users-help@xxxxxxxxxx; run by ezmlm
- References: <20080328105545.81550.qmail@xxxxxxxxxxx>
On Mar 28, 11:55pm, Atom Smasher wrote:
} Subject: which command failed?
}
} obviously the shell can already parse something like this:
}
} foo || bar && baz | grep abc\|xyz
}
} so, without reinventing the wheel, what's a good way to determine which of
} those commands returned a non-zero exit status to the shell?
pipestatus <S> <Z>
An array containing the exit statuses returned by all commands in
the last pipeline.
You can't easily tell which of foo or bar may have returned a non-zero
status, but you can tell (in that particular example) when *both* of them
did, because $pipestatus will have only one element. If $pipestatus has
two elements, then one of baz or grep returned nonzero. Of course in
that case the entire pipeline has nonzero status if and only if grep was
the command that returned nonzero.
Beyond that, you'd have to rewrite the expression so that you can capture
$? in a shell variable before executing the next command.
if if ! foo
then
foo_status=$?
true
fi ||
if ! bar
then
bar_status=$?
false
else true
fi
then
echo | false
echo_status=$pipestatus[1]
grep_status=$pipestatus[2]
fi
That can be collapsed back down to and || && again by using { }.
Messages sorted by:
Reverse Date,
Date,
Thread,
Author