Reevaluate a variable in a script for accurate timestamps
I have a backup script that I want to log when it starts and finishes. So far, the script works great, but the timestamps are identical regardless of how long the backup actually takes. This is due to a variable no being reevaluated.
This is my variable for the timestamp TS=`date +%Y%m%d-%T` The variable is used within a function (tai64n stuff works fine) TIMESTAMP () { if [ "$TS" = "tai64n" ] then echo "$1" | tai64n else echo "$TS $1" fi } Then I use this line to log when it started: TIMESTAMP "jsave: backup started" >>$LOG 2>&1 and after completion, I use a similar line: TIMESTAMP "jsave: backup succeeded" >>$LOG 2>&1 But as I said, the timestamps are identical. 20081211-14:58:37 jsave: backup started 20081211-14:58:37 jsave: backup succeeded How can I get the $TS variable reevaluated? It seems it should be easy but I've been searching to no avail. Thank you :) |
Put the TS line inside your function? BTW, you can use 'logger' for generating syslog-like timestamps as well.
|
I think setting the TS variable within the function would work, but it's actually set in a separate config file. I'll look into logger, thank you.
|
Look at this line:
echo "$1" | tai64n This pipes the output of the argument to a tai64n program. Is that what you intended? Do you maybe want: echo "$1" | tai64nlocal. I though that tai64n was a format. In this line, $TS seems to be a flag setting [ "$TS" = "tai64n" ] Do you want "$(tai64n)" or `tai64n`? Whereas in this line it contains the timestamp: echo "$TS $1" |
Thank you both, I actually thought of a solution while writing my response. I can source the config file again in the function, it works!
{ . $PROGDIR/jsave.conf if [ "$TS" = "tai64n" ] then echo "$1" | tai64n else echo "$TS $1" fi } |
All times are GMT -5. The time now is 10:02 AM. |