B
B
Bodik2011-08-02 16:24:48
bash
Bodik, 2011-08-02 16:24:48

Bash script output to screen and log?

What is the _correct_ way to make the output of the bash script launched by ruakmi to the screen and to the log file? I do it in my own way (I will describe below), but which way is kosher?
A piece of code how I do it now:

slog=/yyy/zzz/xxx.log

function main
{

echo "code here"

}

main 2>&1 | tee -a $slog

Answer the question

In order to leave comments, you need to log in

6 answer(s)
O
ob1, 2011-08-02
@ob1

Here is a more interesting option for you. Put the following file somewhere, for example in /etc/functions/teebash:

if [ "s"${TBASHLOG__} = "s" ] ; then
    TBASHLOG__="/tmp/tbash.log"
fi

if [ ! ${TEEBASH__} ] ; then
    TEEBASH__=1
    . $0 "[email protected]" | tee -a ${TBASHLOG__}
    exit $?
fi

In the first executable line of the script put:
. /etc/functions/teebash

Below is an example of a script on which I tested it:
#!/bin/sh

#TBASHLOG__=/tmp/tbash2.log

. /etc/functions/teebash

echo `date` Is teebash working fine?

In fact, the code from the first listing can simply be inserted at the beginning of any script. But it doesn't turn out so elegant, does it? ;-}

B
bdmalex, 2011-08-02
@bdmalex

If I understand you correctly, then:
tee is a command that displays, or redirects, the output of the command and copies it to a file or variable.

I
icc, 2011-08-02
@icc

Try like this: bash -x ./script.sh 2>&1 | tee script.log

O
ob1, 2011-08-02
@ob1

Bodik , you have a normal way if you need to do this one-time in one script. If there are many such scripts, then I would suggest making an additional /bin/bashtee script , for example:

#!/bin/sh

. "[email protected]" | tee -a /var/lob/bashtee.log

You can do it smarter, but is it necessary?

H
haegor, 2014-12-08
@haegor

In general, there is such a utility as logger, which sends syslog messages.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question