Re: Logging of commands in a bash script to a file
On Tue, 22 Apr 2014 19:06:41 -0300
Daniel Bareiro <daniel-listas@gmx.net> wrote:
>
> Hi all!
>
> I'm writing a bash script that runs several routing commands. I would
> like these commands, on a part of the script, plus run, are saved to a
> log file.
>
> I guess maybe it could be done by putting the commands in a variable
> and then do:
>
> $CMD
> echo $CMD
>
>
> But perhaps there is a more elegant way to do this in a single line.
Make a function in the bash script something like this:
function do_command(){
echo -n `date` >> $logfile
echo $1 $2 $3 $4 $5 $6 >> $logfile
$1 $2 $3 $4 $5 $6
}
If you want to record the output of each command, you'd change the
final line to $1 $2 $3 $4 $5 $6 >> $logfile
In the main script, you'd call it like this:
do_command sudo mount -a
I haven't tested this, so you'll need to tighten it up a little, and it
could be made more elegant, but it's a starting point.
HTH,
SteveT
Steve Litt * http://www.troubleshooters.com/
Troubleshooting Training * Human Performance
Reply to: