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

...

Unlike most other programming languages, bash functions and scripts can only return a single integer between 0 and 255. By convention a return value of 0 means success (true), and any other return value is an error code (false).

A function can return this value using the return keyword (e.g. return 0). The return value is then stored in the special $? variable, which can be checked by the caller. Since this not very much information, function return values are not often used or checked. Instead, as we've seen, functions are often called for their standard output, which serves as a return value proxy.

...

Tip
titleTip

We will do this in a new tmux or screen session, since accidentally calling exit at top-level (instead of in a sub-shell) will log you off the server!

See this nice tmux cheat sheet: http://atkinsam.com/documents/tmux.pdf


Code Block
languagebash
# Invoke tmux from your login command line
tmux new

# Now you're in a tmux. Mine has a green bar at the bottom
( exit 0 )
echo $?

( exit 255 )
res=$?
echo "exit code: $res"

# exit tmux session
exit

...


# You're back at your login command line now

More on capturing output

We've already seen some examples of capturing output from echo using backtick evaluation. Now let's read the contents of a file into a variable using parentheses evaluation.

...

Rather than checking an exit code, it is often more robust to sanity check the returned output; for example, checking to see if it is empty:. If you execute this in your tmux, be sure to enclose it all in parentheses or else your tmux will exit!

Code Block
languagebash
dat=$( cat not_a_file )

if [[ "$dat" == "" ]]; then
  echo "ERROR: no data found" 1>&2; exit 255
else
  echo "Data is: '$dat'"
fi

# or using -z to test for an empty string
if [[ -z "$dat" ]]; then echo "ERROR: no data found" 1>&2; exit 255;  
else echo "Data is '$dat'"; fi

See https://www.gnu.org/software/bash/manual/html_node/Bash-Conditional-Expressions.html for conditional expressions, and https://www.gnu.org/software/bash/manual/html_node/Conditional-Constructs.html for conditional constructs such as if or case.

Setting environment variables for a script

...

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.

...