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"
?
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.
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).
&>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).
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:
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With