Skip to content

Commit 8260be1

Browse files
committed
functions: Run command and print on failure
Created a function to run a command and only print the output in case of failure. This can greatly reduce noice from verbose scripts and commands such as those related to autotools. Signed-off-by: Lars Erik Wik <lars.erik.wik@northern.tech>
1 parent bc78338 commit 8260be1

File tree

1 file changed

+19
-0
lines changed

1 file changed

+19
-0
lines changed

build-scripts/functions

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -596,5 +596,24 @@ rm_if_empty()
596596
fi
597597
}
598598

599+
# Use this function on verbose commands to silience the output unless it returns
600+
# a non-zero exit code
601+
run_and_print_on_failure()
602+
{
603+
local temp_output_file=$(mktemp)
604+
local exit_code=0
605+
if ($@ > "$temp_output_file" 2>&1); then
606+
: # NOOP
607+
else
608+
exit_code=$? # Store exit code for later
609+
echo "Error: Failed to run:" "$@"
610+
echo "--- Start of Output ---"
611+
cat "$temp_output_file"
612+
echo "--- End of Output (Error Code: $exit_code) ---"
613+
fi
614+
615+
rm -f "$temp_output_file"
616+
return $exit_code
617+
}
599618

600619
_IS_FUNCTIONS_SOURCED=yes

0 commit comments

Comments
 (0)