[colug-432] Exit Status of First Program in Bourne Shell Pipeline

Rick Hornsby richardjhornsby at gmail.com
Fri Dec 4 12:24:52 EST 2015


> On Dec 4, 2015, at 10:45, jep200404 at columbus.rr.com wrote:
> 
> How would you improve the following script?
> 
>    #!/bin/sh
> 
>    # This script executes the arguments as a command,
>    # then outputs whether or not command output anything and
>    # whether or not command failed (crashed).
>    #
>    # Two lines are output:
>    # First line:
>    #     1 means command output something
>    #     0 means command did not change anything
>    # Second line:
>    #     1 means command failed (non-zero exit status)
>    #     0 means command exited normally
> 

Forgive me because I’m not entirely sure I understand your goal.  It seems like you’re writing a wrapper that runs a script and then checks to see whether it succeeded or not.

Maybe something like this?

command_output=$($@ 2> /dev/null)
exit_code=$?

# First line
if [ -z "$command_output" ]; then
        echo 0
else
        echo 1
fi

# Second line
if [ $exit_code -eq 0 ]; then
        echo 0
else
        echo 1
fi





More information about the colug-432 mailing list