output when command exits with non-zero status

For obvious reasons, entering the following on the command line results in non-zero status error.:
"echo AWK might do it | sed 's/it*$//' | grep it?"

How do I get Terminal to tell me the command that generated this error

MAC pro, Mac OS X (10.4.11)

Posted on Aug 19, 2009 4:31 AM

Reply
4 replies

Aug 19, 2009 9:38 AM in response to Noaks

In case my goal isn't clear, what I's like to see returned from the example I gave is:


"echo AWK might do it | sed 's/it*$//' | grep it"
"AWK might do it"


Note: there was an extraneous "?" in the original post

Maybe that should be called the command argument. Whatever, if that were to be contained in the result returned, I could probably parse it into what I want easily enough.

Message was edited by: Noaks

Aug 19, 2009 8:39 PM in response to Noaks

The Terminal is not responsible for error messages, it is your shell. The default Mac OS X shell is bash
From the bash man page (man bash):

The return status of a pipeline is the exit status of the last command,
unless the pipefail option is enabled. If pipefail is enabled, the
pipeline's return status is the value of the last (rightmost) command
to exit with a non-zero status, or zero if all commands exit success-
fully.

But I must warn you that if you enable pipefail all the time, you might break existing scripts that do not expect that behavior.

Looking at your echo|sed|grep command line, there are no errors, not even grep. A non-zero exit status is not necessarily an error, as it is all in the eye of the beholder. From the grep man page:

Normally, exit status is 0 if selected lines are found and 1 otherwise.
But the exit status is 2 if an error occurred, unless the -q or --quiet
or --silent option is used and a selected line is found.

So in the case of grep an exit status of 2 means there was an error. A value of 0 or 1 means there was no error, but rather the difference between finding what you were looking for vs not finding.

This thread has been closed by the system or the community team. You may vote for any posts you find helpful, or search the Community for additional answers.

output when command exits with non-zero status

Welcome to Apple Support Community
A forum where Apple customers help each other with their products. Get started with your Apple Account.