Versions Compared

Key

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

...

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"
    echo_se ".. logging to $logFilePath"
    exec 1> >(tee "$logFilePath") 2>&1
  else
    echo_se "** ERROR in autoLogauto_log: no logFile argument provided"
    exit 255
  fi
}

...

Expand
titleSolution

Executing:

Code Block
languagebash
~/workshop/step_02.sh testAutolog test1

produces this output:

Code Block
.. logging to ./autoLog_test1.log

1) Call stdStreams with output and error text:
to standard output: 'text for standard output'
to standard error:  'text for standard error'

2) Capture echo output in a variable and display it:
   echo output was:
text for standard output

3) Call echo_se with error text:
text for standard error

4)Capture echo_se function output in a variable and display it:
text for standard error
echo_se output was: ''

Why are both standard error and standard output text strings displayed in segment 1) when stdStreams is called?

Expand
titleSolution

Because stdStreams echo's to both standard error and standard output in the current execution environment, where both are directed to the terminal by auto-logging.

Why does the $output variable contain the outTxt the output text in 2) when echo is called with output being captured ?

Expand
titleSolution

Because backtick evaluation replaces the command in backticks ( `...` ) with the command's standard output.

...

Expand
titleSolution

The log file produced is autoLog_test1.log, written to the current directory in force when step_02.sh was called. This name is based on the logFileTag we specified as "test1".

Its contents (below) are nearly the same as when ~/workshop/step_02.sh testAutoLog test1 is called, except that the ".. logging to ./autoLog_test1.log" line is not reported, since because that was written before automatic logging was started.

Code Block
1) Call stdStreams with output and error text:
to standard output: 'text for standard output'
to standard error:  'text for standard error'

2) Capture echo output in a variable and display it:
   echo output was:
text for standard output

3) Call echo_se with error text:
text for standard error

4)Capture echo_se function output in a variable and display it:
text for standard error
echo_se output was: ''

...