Files
Ciro Santilli b3f2ddd629 porting "done"
2018-09-09 15:08:44 +01:00

28 lines
724 B
Bash
Executable File

#!/usr/bin/env bash
# echo a command and eval it.
# Can also print the command to a file.
set -e
a=
while getopts a OPT; do
case "$OPT" in
a)
# Append to file instead of overwriting.
a=-a
;;
?)
exit 2
;;
esac
done
shift "$(($OPTIND - 1))"
cmd="$1"
outfile="${2:-/dev/null}"
mkdir -p "$(dirname "$outfile")"
echo "$cmd" | tee $a "$outfile"
# PIPESTATUS so that cmd="main_cmd | post_process" will return the status of cmd.
# Not POSIX.
# https://unix.stackexchange.com/questions/14270/get-exit-status-of-process-thats-piped-to-another
# https://stackoverflow.com/questions/1221833/pipe-output-and-capture-exit-status-in-bash
eval "${cmd}; cmd_exit=\${PIPESTATUS[0]}"
exit "$cmd_exit"