Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.

...

To capture the standard output of parentheses evaluation, the parentheses expression can be "evaluated" with a dollar sign ($). Consider:

  • today=`date +%Y_%m_%d``date`
  • today=$(date +%Y_%m_%d)
    • because it is enclosed in parentheses, the date command is run in a sub-shell, writing its data to its standard output
    • date's standard output stream is connected to the calling shell's standard input by the dollar sign ($) before the opening parenthesis.
  • In both cases the caller's standard input text is stored in the today variable

...

Code Block
languagebash
# Sets up auto-logging to a log file in the current directory
# using the specified logFileTag (arg 1) in the log file name.
auto_log() {
  local logFileTag="$1"
  if [[ "$logFileTag" != "" ]]; then
    local logFilePath="./autoLog_${logFileTag}.log"
    maybe_echo ".. logging to $logFilePath"
    exec 1> >(tee "$logFilePath") 2>&1
    res=$?
    if [[ "$res" != "0" ]]; then
      echo_se "** ERROR: auto logging returned non-0 exit code $res"
      exit 255
    fi
  else
    echo_se "** ERROR in autoLog: no logFile argument provided"
    exit 255
  fi
}

exercise 1

In a sub-shell, test the auto_log function – with and without a logFileTag argument – and check the exit code.

...