Hiding the output of a shell command usually involves redirecting stderr and stdout. Is there any builtin facility or command which by default hides the output but on error dumps all the accumulated output? I would like to run this as a wrapper for remote ssh
commands. Now I have them using redirection but I don't get a clue as to what made them fail, and they are just too verbose.
EDIT: In the end I created the following template based on the answer by @Belmin which I tweaked a little bit to accumulate all the previous commands from the script, use the current process identifier, automatically remove the log, and add a failure red error message when something goes wrong. In this template the initial silent
wrappers will succeed, then fail the third command because the directory already exists:
#!/bin/sh
set -e
SILENT_LOG=/tmp/silent_log_$$.txt
trap "/bin/rm -f $SILENT_LOG" EXIT
function report_and_exit {
cat "${SILENT_LOG}";
echo "\033[91mError running command.\033[39m"
exit 1;
}
function silent {
$* 2>>"${SILENT_LOG}" >> "${SILENT_LOG}" || report_and_exit;
}
silent mkdir -v pepe
silent mkdir -v pepe2
silent mkdir -v pepe
silent mkdir -v pepe2
2>&1
, something like:$* >>"${SILENT_LOG}" 2>&1" || report_and_exit