LinuxQuestions.org
Latest LQ Deal: Complete CCNA, CCNP & Red Hat Certification Training Bundle
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - General
User Name
Password
Linux - General This Linux forum is for general Linux questions and discussion.
If it is Linux Related and doesn't seem to fit in any other forum then this is the place.

Notices


Reply
  Search this Thread
Old 12-11-2008, 06:17 PM   #1
gojamiegirl
LQ Newbie
 
Registered: Aug 2004
Location: Wildomar, California
Distribution: Arch Linux
Posts: 12

Rep: Reputation: 0
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
 
Old 12-12-2008, 01:38 PM   #2
unSpawn
Moderator
 
Registered: May 2001
Posts: 29,361
Blog Entries: 55

Rep: Reputation: 3548Reputation: 3548Reputation: 3548Reputation: 3548Reputation: 3548Reputation: 3548Reputation: 3548Reputation: 3548Reputation: 3548Reputation: 3548Reputation: 3548
Put the TS line inside your function? BTW, you can use 'logger' for generating syslog-like timestamps as well.
 
Old 12-12-2008, 05:06 PM   #3
gojamiegirl
LQ Newbie
 
Registered: Aug 2004
Location: Wildomar, California
Distribution: Arch Linux
Posts: 12

Original Poster
Rep: Reputation: 0
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.
 
Old 12-12-2008, 05:33 PM   #4
jschiwal
LQ Guru
 
Registered: Aug 2001
Location: Fargo, ND
Distribution: SuSE AMD64
Posts: 15,733

Rep: Reputation: 677Reputation: 677Reputation: 677Reputation: 677Reputation: 677Reputation: 677
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"
 
Old 12-12-2008, 11:50 PM   #5
gojamiegirl
LQ Newbie
 
Registered: Aug 2004
Location: Wildomar, California
Distribution: Arch Linux
Posts: 12

Original Poster
Rep: Reputation: 0
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
}
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Calling perl script and passing variable from php script hosea Programming 5 10-21-2008 09:01 AM
Help: removing a variable substring from a string variable in sh script gnparsons Programming 2 06-04-2008 06:21 PM
how to pass a variable from a called script back to the calling script steven.c.banks Linux - General 2 05-05-2008 03:00 PM
Getting file timestamps from a shell script sidra Programming 9 05-15-2007 09:55 AM
setting a variable variable in a script... this works, but could it be more elegant? pwc101 Programming 3 08-18-2006 12:23 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - General

All times are GMT -5. The time now is 07:19 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration