I have to do something like :

In a folder /x everyday 10Pm and next day 3 AM, 2 files are placed. I have to write a script which will check the folder /x and confirm if the files are there or not and send mails accordingly everyday.

file formats:

  • ValuationIR**02092013**_0326 (FileName pattern: ValuationIR<date>_<time>.csv )
  • SnapshotIR**02082013**_2238 (FileName pattern: SnapshotIR<date>_<time>.csv )

If you simply need to check if the two files exist and send an email if they do not, something like this should do nicely:

test -f file1 -a -f file2 || ( echo "Either file1 or file2 doesn't exist" | sendmail ... )

The paranthesis are not strictly necessary, but I'm including them for clarity. You can also use bash functions to make this a bit more readable and easily expandable, but that won't be shell-agnostic (though most shells have ways to do the same thing).

send_mail() {
    echo "message text" | sendmail ...
test -f file1 -a -f file2 || send_mail

This can be further expanded on if necessary, but the above should get you started.

When the script itself works as intended, you can add it to your user crontab to run it nightly at a specified time.

  • 1
    The files have to be searched with the pattern (starts with <SnapshotIR> + contains <previous date>), as there would be earlier dated files as well. – Swagatika Feb 11 '13 at 12:08
  • So make file1 and file2 match your required filenames. You have to do some of the work yourself. – Jason Tan Feb 11 '13 at 12:32
  • And/or add checks to see if the files' content match what you expect. Just expand on the part before || to do whatever checks you want. – user Feb 11 '13 at 12:34

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.