• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 500
  • Last Modified:

Modified date

I got an modified guestbookscript. In this script I need an routine that print out the date plus a number. The number should increse with +1 per time the guestbook updates per day. ex. first input 970618-1, second input 970618-2 and the next day 970719-1.

                      Need help...
0
jonasm
Asked:
jonasm
  • 4
  • 4
1 Solution
 
julio011597Commented:
You should at least state your OS and programming language.

-julio
0
 
jonasmAuthor Commented:
Using Linux and Perl
0
 
julio011597Commented:
Here it is:

--//--
#!/usr/bin/perl

open(LOG, '+< counter.idx'); #open for update
$count = <LOG> + 1;
seek(LOG, 0, 0); #back to BOF
printf LOG $count;
close(LOG);

$year = (gmtime)[5];
$month = (gmtime)[4] + 1; #month and mday are 0 based
$mday = (gmtime)[3] + 1;

printf "$year%02d%02d-%d\n", $month, $mday, $count;

exit(0);
--//--

A few notes:

1. file 'counter.idx' must already exist, but may be empty, so you may create it with just a: touch counter.idx, then chmod 666 to make it updatable by all;

2. you may want to substitute gmtime() with localtime() to get local time instead that GMT time.

HTH, julio
0
Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

 
jonasmAuthor Commented:
when a new day comes the count must restart at ZZXXYY-1 and count up..

/Jonas
0
 
julio011597Commented:
Right, sorry...

since your program cannot be aware of a request to be the first of a day, you should set up cron to help it:

an easy way would be to put an empty - say - counter.0 file that cron could use to overwrite the counter.idx each midnight;

or, cron could delete counter.idx and touch it again;

or cron could run a script that writes a 0 in counter.idx (this needs a little modification of my first script).

I'm not on Linux, and the cron facility is quite dependent on the platform; you may want to have a look at man cron and man crontab for details.
If you have any problems, just feel free to ask more.

Rgds, julio
0
 
jonasmAuthor Commented:
Ok, thanks on this system I don't have permissions to use crontab (argh!). Is there any way to do It inside the script? use another datafile to save the date in?
I need it, really (increased the points to), I would be very happy if you could help me..

rgds

/Jonas
0
 
julio011597Commented:
Yes, another way (maybe the best) is to also store the date into counter.idx:

--//--
#!/usr/bin/perl

open(LOG, '+< test.idx');
chop($oday = <LOG>);
$nday = (gmtime)[3];

if($oday ne $nday) {
  $count = "1";
  seek(LOG, 0, 0);
  printf LOG "%2d\n", $nday;
  printf LOG "%5d", $count;
}
else {
  $count = <LOG> + 1;
  seek(LOG, 0, 0);
  printf LOG "%2d\n", $oday;
  printf LOG "%5d", $count;
}
close(LOG);

$year = (gmtime)[5];
$month = (gmtime)[4] + 1;

printf "$year%02d%02d-%d\n", $month, $nday, $count;

exit(0);
--//--

This should work as expected :)
I put those printf() there, with field width specifiers, to be sure that each update completly overwrites the old data.
The count field width is set to 5: change it if you think you'll get more than 99999 requests a day.

Cheers, julio
0
 
jonasmAuthor Commented:
Exactly what I needed!,
Thanks for the fast replys!

/Jonas
0

Featured Post

How to Use the Help Bell

Need to boost the visibility of your question for solutions? Use the Experts Exchange Help Bell to confirm priority levels and contact subject-matter experts for question attention.  Check out this how-to article for more information.

  • 4
  • 4
Tackle projects and never again get stuck behind a technical roadblock.
Join Now