#!/bin/bash # # cmdout program args... # # Wrapper to label the commands output as being from 'stdout' or 'stderr' # channels, and report the final 'exit status' of the comannd. # # Anthony Thyssen (developed 1988 and hardly changed since) # sed_flag=--unbuffered # echo "CMD: $*" printf "CMD: "; printf " '%s'" "$@"; printf '\n' # quote everything #echo "CMD:" "${@@Q}" # Command in Quotes (Bash v5 only -- arrggh) exec 9>&1 # save stdin (for the report) ( exec 3>&1; # recover status (3) ( exec 2>&1; # save stderr ( "$@"; echo $? >&3 # run command, send status to channel 3 ) | sed $sed_flag 's/^/OUT: /; s/ //g' >&9 # mark stdout ) | sed $sed_flag 's/^/ERR: /; s/ //g' >&9 # mark stderr ) | sed $sed_flag 's/^/STAT: /' # mark status exit 0