Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Logging functions in bash and stdout

I'd like to be able to put log messages in the middle of bash functions, without affecting the output of those very functions. For example, consider the following functions log() and get_animals():

# print a log a message
log ()
{
    echo "Log message: $1"
}

get_animals()
{
    log "Fetching animals"
    echo "cat dog mouse"
}

values=`get_animals`
echo $values

After which $values contains the string "Log message: Fetching animals cat dog mouse".

How should I modify this script so that "Log message: Fetching animals" is outputted to the terminal, and $values contains "cat dog mouse"?

like image 791
Adam Avatar asked Aug 19 '10 18:08

Adam


People also ask

What is $@ in bash?

bash [filename] runs the commands saved in a file. $@ refers to all of a shell script's command-line arguments. $1 , $2 , etc., refer to the first command-line argument, the second command-line argument, etc. Place variables in quotes if the values might have spaces in them.

How do I redirect a script to log file?

Normally, if you want to run a script and send its output to a logfile, you'd simply use Redirection: myscript >log 2>&1. Or to see the output on the screen and also redirect to a file: myscript 2>&1 | tee log (or better still, run your script within the script(1) command if your system has it).

What does &> mean in bash?

&>word (and >&word redirects both stdout and stderr to the result of the expansion of word. In the cases above that is the file 1 . 2>&1 redirects stderr (fd 2) to the current value of stdout (fd 1).


1 Answers

choroba's solution to another question shows how to use exec to open a new file descriptor.

Translating that solution to this question gives something like:

# Open a new file descriptor that redirects to stdout:
exec 3>&1

log ()
{
    echo "Log message: $1" 1>&3
}

get_animals()
{
    log "Fetching animals"
    echo "cat dog mouse"
}

animals=`get_animals`
echo Animals: $animals

Executing the above produces:

Log message: Fetching animals
Animals: cat dog mouse

More information about using I/O redirection and file descriptors in Bash can be found at:

  • Bash Guide for Beginners, section 8.2.3, Redirection and file descriptors
  • Advanced Bash-Scripting Guide, Chapter 20, I/O Redirection
like image 143
David T. Pierson Avatar answered Sep 20 '22 11:09

David T. Pierson