views:

92

answers:

1

I would like to write a small script that does the following (and that I can then run using my crontab):

  • Look into a directory that contains directories whose names are in some date format, e.g. 30-10-09.

  • Convert the directory name to the date it represents (of course, I could put this information as a string into a file in these directories, that doesn't matter to me).

  • Compare each date with the current system time and find the one that has a specific time difference to the current system date, e.g. less than two days.

  • Then, do something with the files in that directory (e.g., paste them together and send an email).

I know a little bash scripting, but I don't know whether bash can itself handle this. I think I could do this in R, but the server where this needs to run doesn't have R.

I'm curious anyway to learn a little bit of either Python or Ruby (both of which are on the server).

Can someone point me in the right direction what might be the best way to do this?

+1  A: 

I would suggest using Python. You'll need the following functions:

  • os.listdir gives you the directory contents, as a list of strings
  • time.strptime(name, "%d-%m-%y") will try to parse such a string, and return a time tuple. You get a ValueError exception if parsing fails.
  • time.mktime will convert a time tuple into seconds since the epoch.
  • time.time returns seconds since the epoch
  • the smtplib module can send emails, assuming you know what SMTP server to use. Alternatively, you can run /usr/lib/sendmail, through the subprocess module (assuming /usr/lib/sendmail is correctly configured)
Martin v. Löwis