0

I have the command below that I used to run without $(), but I have to in order to capture the return code of the remote script. The issue is that now I don't see the command output unless I cat it. Can I see the output during execution?

set -o pipefail
COMMAND=$(ssh ${RMT_HOST} ${RMT_DIR}/${SCRIPT_NAME} ${ARGUMENTS} < /dev/null |& tee -a ${RMT_EXEC_LOG})
RETCODE=$?

EDIT Just to clear why I use $() here is a link

Nir
  • 1,265
  • 7
  • 23
  • 34

1 Answers1

1

If the question you're actually asking is "how can I both capture and display the output of a process?", you're on the right track with tee, but as others have noted, if you later look at the exit code by inspecting $?, you will be getting the exit code of tee and not of the command run through it.

It's easy to store the output and look at it later, but you need to capture the exit code immediately. Better, then, to use a temporary file and handle the output separately:

scratch="$(mktemp)"
trap 'rm -fr "$scratch"' EXIT
--SOME LONG COMMAND-- > "$scratch"
returncode=$?
--HANDLER FOR RETURNCODE--
cat "$scratch" >> /path/to/persistent_logfile
DopeGhoti
  • 73,792
  • 8
  • 97
  • 133