#!/bin/bash
if [ $# -lt 2 ] || [ "$1" != "-c" ]
then
  echo "`basename "$0"` -c <cmd> <arg1> <arg2> ... <argN>" >&2
  exit 1
fi
shift # drop "-c"

LOG="`mktemp`"

# save output to the log file
bash -c "$*" >> "$LOG" 2>&1
RET=$?
# in case of an error output full log to screen
[ "$RET" -ne 0 ] && cat "$LOG"

rm -f "$LOG"
# and return error exit code to the caller
exit "$RET"
