Delete old files x days and send email if its done

2

I have a script that works removing files x days and keep the folders. I'm trying to send an email once its done with the deletion. Any advice? Current script is below:

#!/bin/bash
find /testftp/* -type f -mtime +10 -exec rm {} \;
UBJECT="FTP Cleanup"
EMAIL="myemail@somewhere.com"
EMAILMESSAGE="IT WORKS"
/bin/mail -s "$SUBJECT" "$EMAIL" "$EMAILMESSAGE"

JoyIan Yee-Hernandez

Posted 2012-01-11T18:49:04.517

Reputation: 487

Does that not work? Are you seeing an error, or what? – Zoredache – 2012-01-11T18:52:44.827

That's SUBJECT, not UBJECT. – Daniel Beck – 2012-01-11T19:09:02.647

Answers

2

One problem: you misspelled SUBJECT, but the only problem that will cause is that the message will have an empty subject.

The bigger problem is that /bin/mail reads the message body from standard input, not from a command line argument.

Try this:

SUBJECT="FTP Cleanup"
EMAIL="myemail@somewhere.com"
EMAILMESSAGE="IT WORKS"
echo "$EMAILMESSAGE" | /bin/mail -s "$SUBJECT" "$EMAIL"

Or, for a longer message body:

SUBJECT="FTP Cleanup"
EMAIL="myemail@somewhere.com"
/bin/mail -s "$SUBJECT" "$EMAIL" <<EOF
Message body line 1
Message body line 2
Message body line 3
EOF

Keith Thompson

Posted 2012-01-11T18:49:04.517

Reputation: 4 645

It works Keith! Thanks also for pointing out that typo. Cheers! – JoyIan Yee-Hernandez – 2012-01-11T20:15:45.987