How to create a log file of a script?

hi,
How to create a log file of a script. Like spool does .
I want to create a log file of whatever the script is doing step wise.

like -xvf does or something better then that.

thanks ...

you can have echo statements and redirect the output of echo statements into a file.

It depends on how fancy you want it. Like penchal said you could have echo statements.

#!/bin/bash
file=$1
echo "Info: Displaying file $file"
cat $file

Run that as...

./script test.txt >> test.log

Some scripts do this...

#!/bin/bash
file=$1
log=$2
echo "Info: Displaying file $file" >> $log
cat $file

Run that as...

./script test.txt test.log

A better way is to use 'getopt' or builtin 'getopts' then you could put an optional '-l test.log' on the command line, which would be more clear. Even fancier is to set a loglevel of verbosity. People do that with '-v' multiple times or '-v LEVEL' where LEVEL is optional. Let me know if more examples are needed.

One another suggestion, if you cannot edit the whole script, then try set -x in the first part of the script... and then redirect the output to a file.

But it may be confusing..