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

William Yang wyang at gcfn.net
Fri Dec 4 12:59:48 EST 2015


Am I missing something in your requirements?

	#! /bin/sh

	OUTPUT="`$@`"
	ERRLEVEL=$?

	test "${OUTPUT}" != "" && echo 1 || echo 0
	test "${ERRLEVEL}" != 0 && echo 1 || echo 0


On 12/04/2015 11:45 AM, 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 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
> _______________________________________________
> colug-432 mailing list
> colug-432 at colug.net
> http://lists.colug.net/mailman/listinfo/colug-432
> 


-- 
William Yang
wyang at gcfn.net


More information about the colug-432 mailing list