[plug] howto logging result to a log file

Denis Brown dsbrown at cyllene.uwa.edu.au
Wed Aug 27 06:46:32 WST 2003


On Tue, 26 Aug 2003, Jon Miller wrote:

> In my script the last thing I need to do is create a log file that
> contains the output of the program plus the date and time of the entry.
>
> Since I have no idea how to create a log file other than using (correct
> me if I'm wrong) the >> to append to a file, my question is will this
> add a new entry per line with the date to the left.
> What I'm thinking is the following:
> echo `date` mmpf >> /var/log/pstfxst.log
> Does this seem correct?
>
Hello, Jon.

Well, to create the file in the first place, perhaps "touch"?   Certainly
the >> seems tolerant of the initial absence of a file though.

In bash at least, echo 'date' just echoes the literal word 'date'.   To
get the true date you would need echo $(date) or if you want to get fancy
you can add formatting such as echo $(date +%Y%m%d) to get just the Year,
Month and Day a la 20030827

If it is necessary to capture the start date/time of a process, you might
want to create a variable STARTTIME along the lines of STARTTIME=$(date)
and then export STARTTIME   At a later point you can reference this with
echo $STARTTIME  -- at least it works for me (Debian/GNU, bash... YMMV)
Apologies to shell afficiondos, this is probably very clunky :-(

Russell's noted that the rest of the example string, the "mmpf" part,
would be treated as a literal string so his suggestion for the scripting
seems like the way to go.

HTH,
Denis

PS any suggestions for a good reference text on shell scripting?   Sure I
can Google but a recommendation from someone in our community who's used
the book(s) would be "gold".




More information about the plug mailing list