[Date Prev][Date Next] [Thread Prev][Thread Next] [Date Index] [Thread Index]

Re: Logging of commands in a bash script to a file



Steve Litt wrote, On 04/23/2014 12:07 PM:
> 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.


Good, but I think you can avoid the positional params by just using $@,
as I do here in my "run" function:

https://github.com/chkoreff/Fexl/blob/fresh/src/build#L34



-- Patrick


Reply to: