Versions Compared

Key

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

...

We've replaced the multiple echo lines in usage with a single echo'd string, illustrating that the invisible line feeds in the echo'd text are faithfully preserved. The function also exits with a non-0 ("failure") return code, since no valid command processing was performed.

Code Block
languagebash
function usage() {
  echo "
advanced_bash.sh, version $__ADVANCED_BASH_VERSION__

Usage: advanced_bash.sh <command> [arg1 arg2...]

Commands:
  helloWorld [text to display]
  stdStreams [text for stdout] [text for stderr]
  testAutolog <logFileTag> [text for stdout] [text for stderr]
"
  exit 1255
}

Here's how to call the stdStreams command specifying the text "hello world!" for standard output and "goodbye world!" for standard error.

...

Expand
titleSolution
~/
Code Block
languagebash
bash
~/workshop/step_02.sh stdStreams 'hello world!' "goodbye world!" 2>&1

exercise 3

What is written to the ssout.txt file when the following is executed, and why?

Code Block
languagebash
~/workshop/step_02.sh stdStreams 'hello world!' "goodbye world!" | tee ssout.txt
Expand
titleSolution

Only the standard output text is written:

Code Block
to standard output: 'hello world'

Remember the pipe ("|") conntects one program's standard output (here from the step_02.sh

stdStreams 'hello world!' "goodbye world!" 2>&1

script) to the next program's standard input (here to the tee program). Standard error is ignored by the pipe (unless redirected).

More Parts - automatic logging

...

  • exec 1> causes redirection of all standard output for the duration of the current shell environment (here the script).
  • the 2>&1 at the end is our normal "redirect standard error to standard output" idiom
  • >(tee "my_logfile.log") is the magic that says to write standard output (which now includes standard error text) to a file via tee.
    • the >( ) syntax also sends all the current execution environment's standard output to a sub-shell (more on this later), which
    • appears to be necessary because otherwise tee would just act on its piped-in standard input.

auto_log function

The new auto_log helper function sets up automatic logging to a log file in the current directory, named using a tag string specified as its 1st argument.

...

So why is echo -e used in the testAutoLog function? Because the -e argument to echo enables interpretation of backslash escapes. For example, "\n" will be interpreted as a newline, and "\t" as a tab character.

exercise

...

4

Call the testAutoLog command with no further command line arguments. What happens, and why?

Expand
titleSolution

Executing:

Code Block
languagebash
~/workshop/step_02.sh testAutoLog

produces this output:

Code Block
** ERROR in autoLog: no logFile argument provided

No further code is executed after the auto_log helper function detects that no tag string has been provided, because it calls exit.

exercise

...

5

What output is produced when you call the testAutoLog command with a tag string of "test1".

...