mail_error 514 B

12345678910111213141516171819202122
  1. #!/bin/bash
  2. # mail_error -o file -m email_address command
  3. # Runs command and redirects all output to file
  4. # If command rc != 0, sends file to email_address
  5. function die { echo $1 ; exit 1; }
  6. while getopts "o:m:" c; do
  7. case $c in
  8. o) output_file=$OPTARG;;
  9. m) email=$OPTARG;;
  10. \*) echo "oops";;
  11. esac
  12. done
  13. shift $(($OPTIND-1))
  14. echo "output_file=$output_file email=$email"
  15. [[ -z $1 ]] && die "Must specify command"
  16. if ! "$@" > $output_file 2>&1 ; then
  17. mail -s "mail_error fail: $@" $email < $output_file
  18. fi