[colug-432] Exit Status of First Program in Bourne Shell Pipeline
jep200404 at columbus.rr.com
jep200404 at columbus.rr.com
Fri Dec 4 11:45:31 EST 2015
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 exitted normally
pipe=~/command_to_wc.pipe
rm -f "${pipe}"
mknod "${pipe}" p
"$@" >"${pipe}" 2>/dev/null &
pid="$!"
n=`wc -c <"${pipe}"`
if [ "$n" = '0' ]; then
echo 0 ;# command did not output anything
else
echo 1 ;# command output something
fi
if wait $pid; then
echo 0 ;# normal
else
echo 1 ;# failure
fi
rm -f "${pipe}"
I started with
n=`"$@" | wc -c`
but I could not get the exit status of the "$@" command,
so I split the pipeline into to two chunks using the named pipe
to communicate between them. Can I get rid of the named pipe?
Need to:
run in plain old boring (but oh so portable) Bourne shell
so avoid nifty bash features such as but not limited to
PIPESTATUS
pipefail
More information about the colug-432
mailing list