Files
sharedinbox/scripts/silent_on_success.sh
2026-04-25 07:07:05 +02:00

25 lines
524 B
Bash
Executable File

#!/usr/bin/env bash
# Run a command silently. On failure, replay captured output and exit with the original code.
# When DEBUG is set, print start time, command, end time, and duration.
tmp=$(mktemp)
trap 'rm -f "$tmp"' EXIT
if [ -n "${DEBUG:-}" ]; then
start=$(date +%s)
echo "[$(date '+%H:%M:%S')] + $*"
fi
if "$@" > "$tmp" 2>&1; then
code=0
else
code=$?
cat "$tmp"
fi
if [ -n "${DEBUG:-}" ]; then
end=$(date +%s)
echo "[$(date '+%H:%M:%S')] = $* (${code}) $((end - start))s"
fi
exit $code