mail_error 469 B

123456789101112131415161718192021
  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. [[ -z $1 ]] && die "Must specify command"
  15. if ! "$@" > $output_file 2>&1 ; then
  16. mail -s "mail_error fail: $1" $email < $output_file
  17. fi