I Need to Automate the script to get a mail. Example when we are running shell script all the files are present or not then if present in mail need to get the message files are present else xxxxxxxxx.csv files alone not present please check .I need to write the script like this, please suggest.

Regards, Sindhu

  • Which files where what? Incomprehensible. Question doesn't make sense.
    – RobertL
    Nov 26, 2015 at 7:32
  • Hi Robert, here my query is how to automate the mail,Example In server 1 have i have automated to get files and alter need to automate the workfow... if files are not present in respective directories then workflow should not run.. in mail i should get like file a is not persent ....
    – sindhu
    Nov 26, 2015 at 7:38
  • You want the script to send an email about missing files? Is the mail system running, you just want to know how to send email from a script?
    – RobertL
    Nov 26, 2015 at 7:41
  • What particular part of the script are you having a problem with? Testing for file existence? Generating the email body text?
    – forquare
    Nov 26, 2015 at 7:42
  • Yeah i need to know the missing files and i need to now how to send such mail from script too
    – sindhu
    Nov 26, 2015 at 7:42

2 Answers 2


Your question is very unclear, but from what I can make out, you're trying to run something periodically and want to get an email notification when something goes wrong, right?

If so, just make your shell script print error messages, and run it from cron; cron will automatically send you an email if a script produces any output. For more information on how to so, run man crontab and man 5 crontab, and read that documentation.

    for f do command <"$f" &&
          set -- "$@" "$f"
    done; printf present:\\t%s\\n "$@"
}   2>&1

For sending mail from the shell you can use mail, but you'll need a properly configured MTA.

Many programs will send your unix user account an email, but this might very well end up in dead_letter if you've not configured this correctly. Among these are cron, as has been mentioned, but there is also at, and even ex or vi can do so, both of which can run shell scripts.

You can use the above function with a standard mail client like:

if_files ./* |
mail -s file_check [email protected]
  • Why the function? Is this not the same as ls -1 ./*|mail -s file_check [email protected]?
    – Lambert
    Nov 26, 2015 at 8:17
  • @Lambert - no, it is not the same. as run, maybe so, but the < open will also complain for files which are not readable by the user that runs it.
    – mikeserv
    Nov 26, 2015 at 8:19

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .