If you are using bash just execute the file with the -x flag. Consider the following:
$ cat t.sh
#!/bin/bash
echo "Foo" >&2
echo "Bar" >&1
$ bash t.sh
Foo
Bar
$ bash -x t.sh
+ echo Foo
Foo
+ echo Bar
Bar
$
You can see that not all context will be saved (i.e. where output is being redirected) but you will see each line of the file as it executes with a + preceding it.
L7Sqr
Practically a Posting Shark
851 posts since Feb 2011
Reputation Points: 253
Solved Threads: 155
Skill Endorsements: 7
What you are saying is not making much sense. You are first requesting to log the output of your program then you are asking that whatever is performed within that script should also be written to a log. I also dont understand the concept of recursive logging.
In general, logging mechanisms are built-in to the software that uses them not bolted on after the fact. If you want to write a script that does logging, write your own function to do it and use that:
#!/bin/bash
LOG_FILE="the.log"
function logit {
echo "[${USER}][`date`] - ${*}" >> ${LOG_FILE}
}
logit "This gets written to the log"
However, that does not catch the output of the program like you wish. You could adapt it if you like:
#!/bin/bash
LOG_FILE="the.log"
function logit {
echo "[${USER}][`date`] - ${*}" >> ${LOG_FILE}
}
while read line; do
logit ${line}
echo ${line}
done
That will simply read all input, log it, then propagate the output to the screen (or other output device).
I simply dont see a way to combine the two into a single utility at the script level.
L7Sqr
Practically a Posting Shark
851 posts since Feb 2011
Reputation Points: 253
Solved Threads: 155
Skill Endorsements: 7
Question Answered as of 1 Year Ago by
L7Sqr