vili has asked for the wisdom of the Perl Monks concerning the following question:

Greetings Enlightened ones,
I have a script that takes a epoch timestamp as an argument. I'd like to automate it by incrementing the previous timestamp
by 86400, and have the crond handle it daily, but am not real sure how to go about it. I guess I need to keep track of the last timestamp
in a file or something... Please help me work out the details. Your assistance is appreciated
~vili
"We're smarter individually." -- Larry Niven

Replies are listed 'Best First'.
Re: Automate Parsing Script
by kesterkester (Hermit) on Sep 04, 2003 at 17:32 UTC
    Not elegant, but it works: put the initial timestamp in a file called timestamp.txt. Then run the following subroutine on it... the first block reads in the timestamp, the last block writes it back out, and you add a day's worth of seconds in between.
    #!/usr/bin/perl use warnings; use strict; sub add_one_day { my $timestamp_file = 'timestamp.txt'; open my $read_fh, '<', $timestamp_file; chomp ( my $timestamp = <$read_fh> ); close $read_fh; $timestamp += 86400; open my $write_fh, '>', $timestamp_file; print $write_fh "$timestamp\n"; close $write_fh; }
Re: Automate Parsing Script
by blue_cowdawg (Monsignor) on Sep 04, 2003 at 17:25 UTC

        incrementing the previous timestamp by 86400, and have the crond handle it daily, but am not real sure how to go about it. I guess I need to keep track of the last timestamp in a file or something..
    The answer lies within you....

    Here is one way of doing it. There are many:

    #!/usr/bin/perl -w ####################################################### use strict; use warnings; use diagnostics; use FileHandle; my $fh=new FileHandle("< /var/run/mystamp.txt") or die "/var/run/mystamp.txt: $!"; my $stamp=<$fh>; chomp $stamp; undef $fh; $stamp += 86400; $fh=new FileHandle("> /var/run/mystamp.txt") or die "/var/run/mystamp.txt:$!"; printf $fh "%d\n",$stamp; undef $fh; exit(0);

    That is an oversimplified IMHO way of doing it and let me outline some of the issues with this method:

    • No file locking. If another instance of this script starts up then they both will attempt to update the file.
    • No checking of the data in the file for validity
    Probably other problems, but that's just first pass.

    What you didn't say in your post is why you are doing this in the first place. In your script that uses epoch time stamps why not use time system call to generate your epoch stamp?


    Peter L. Berghold -- Unix Professional
    Peter at Berghold dot Net
       Dog trainer, dog agility exhibitor, brewer of fine Belgian style ales. Happiness is a warm, tired, contented dog curled up at your side and a good Belgian ale in your chalice.
Re: Automate Parsing Script
by rob_au (Abbot) on Sep 05, 2003 at 02:08 UTC
    Whilst blue_cowdawg and kesterkester have already answered this question, I thought I would suggest yet-another-way-to-do-it using a one-liner that could be run directly from the crontab ...

    perl -i -pe 's/(\d+)/$1 + 86400/e' timestamp.txt

     

    perl -le "print+unpack'N',pack'B32','00000000000000000000001010000001'"

Re: Automate Parsing Script
by vili (Monk) on Sep 04, 2003 at 18:04 UTC
    Thanks a lot to both of you. I'm sorry for being such a lazy ass.
    ~vili