Pipe/redirect a group of commands

I currently use the following setup to redirect the output of multiple commands:

echo "Some normal commands"
(
echo "Error: something happened"
echo "Warning: this incident will be logged"
) >> logfile
echo "More normal commands"

This is pretty useful, and it also works with pipes.

Is this the best way to do this? Is there an alternative that I should consider?

Answers:

Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.

Method 1

The alternative is to use braces instead of parentheses. This change executes the commands in the current shell, not in a subshell

echo "Some normal commands"
{
echo "Error: something happened"
echo "Warning: this incident will be logged"
} >> logfile
echo "More normal commands"

ref: https://www.gnu.org/software/bash/manual/bashref.html#Command-Grouping

This is particularly relevant when you’re modifying variables inside the group:

$ x=5; ( x=10; echo inside: $x; ); echo outside: $x
inside: 10
outside: 5

$ x=5; { x=10; echo inside: $x; }; echo outside: $x
inside: 10
outside: 10

Method 2

Glenn’s answer is a good one — the distinction between ( ... ) and { ... } is important.

One strategy I often use for error output like what’s in your question is the tee command. You could do something like this:

echo "Normal output"
{
  printf "[%s] %sn" "$(date '+%Y-%m-%d %T')" "Warning text"
  printf "[%s] %sn" "$(date '+%Y-%m-%d %T')" "This event is logged."
} | tee -a $logfile >&2
echo "More normal output"

The tee command will send output to two places; -a option “appends” output to the named file, and the command will also pass input along to stdout. The >&2 at the end of the line redirects tee‘s stdout to stderr, which may be handled differently (i.e. in a cron job).

One other tip that I often use in shell scripts is to change the behaviour of debug or verbose output based on whether the script is running on a terminal or has a -v option provided. For example:

#!/bin/sh

# Set defaults
if [ -t 0 ]; then
  Verbose=true; vflag="-v"
else
  Verbose=false; vflag=""
fi
Debug=false; AskYN=true; Doit=true

# Detect options (altering defaults)
while getopts vdqbn opt; do
  case "$opt" in
    v)  Verbose=true; vflag="-v" ;;             # Verbose mode
    d)  Debug=true; Verbose=true; vflag="-v" ;; # Very Verbose
    q)  Verbose=false; vflag="" ;;              # quiet mode (non-verbose)
    b)  AskYN=false ;;                          # batch mode
    n)  Doit=false ;;                           # test mode
    *)  usage; exit 1 ;;
  esac
done

# Shift our options for further processing
shift $(($OPTIND - 1))

$Verbose && echo "INFO: Verbose output is turned on." >&2
$Debug && echo "INFO: In fact, expect to be overrun." >&2

# Do your thing here
if $AskYN; then
  read -p "Continue? " choice
  case "$choice" in
    Y|y) $Doit && somecommand ;;
    *) echo "Done." ;;
  esac
fi

Scripts can start with something generic like this at the top, with Verbose and Debug output spattered throughout the script. It’s just one way to do it — there are many, and different folks will all have their own way to handle this stuff, especially if they’ve been around a while. 🙂

One more option is to handle your output with a “handler” — a shell function that may do more intelligent things. For example:

#!/bin/bash

logme() {
  case "${1^^}" in
    [IN]*)  level=notice ;;
    W*)     level=warning ;;
    A*)     level=alert ;;
    E*)     level=emerg ;;
    *)      level=notice ;;
  esac
  if [[ "$#" -eq 1 ]]; then
    # Strip off unnecessary prefixes like "INFO:"
    string="${1#+([A-Z])?(:) }"
  else
    shift
    string="<a href="https://getridbug.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="5f7b1f">[email protected]</a>"
  fi
  logger -p "${facility}.${level}" -t "$(hostname -s)" "$string"
}

echo "Normal output"
logme INFO "Here we go..."
somecommand | logme
echo "Additional normal output"

(Note that ${var^^} is bash-only.)

This creates a shell function which may uses your system’s syslog functions (with logger command) to send things to system logs. Thelogme()` function can be used either with options that generate single lines of log data, or with multiple lines of input that are processed on stdin. Play with it if it seems appealing.

Note that this is an example and probably should not be copied verbatim unless you understand it and know that it does precisely what you need. A better idea is to take the concepts here and implement them yourself in your own scripts.

Method 3

More appropriate way of doing this is with { command; } rather than (command). The reason is that when commands are grouped with () a subshell is opened to execute those commands and thus the variables that are initialized during that block will not be available to other sections of the script.

Instead when we use {} for command grouping, the commands are executed within the same shell and thus the variables will be available to other sections of the script.

echo "Some normal commands"

{
    var=1
    echo "Error: something happened"
    echo "Warning: this incident will be logged"
} >> logfile

echo "The value of var is: $var"
echo "More normal commands"

Here, when this section is executed the $var variable retains its value, where as in the other case it will not.


All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

0 0 votes
Article Rating
Subscribe
Notify of
guest

0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x