prim: Introduces Python version
[lcapit-junk-code.git] / libcom.sh
blob087fa680081be8f3e2eae096cfa38878e51a2c44
1 ##
2 ## Common used functions library
3 ##
5 # die
7 # $1: Exit code
8 # $2: Error message
10 # Returns: nothing
12 # Print the error message in standard error output
13 # and exit
15 die()
17 exitcode=$1
18 str="$2"
20 echo -e "Error: $str" >> /dev/stderr
21 exit $exitcode
25 # die_on_error
27 # $1: Return code
28 # $2: Error message
30 # Returns: nothing
32 # Calls die() if return code is not zero
34 die_on_error()
36 retcode=$1
37 errmesg="$2"
39 if [ $retcode -ne 0 ]; then
40 die 1 "$errmesg"
44 # send_email_report
46 # $1: Subject
47 # $2: E-mail contents
49 # Returns: sendmail's exit code
51 # Send an e-mail to the address specified in the TO_ADDR variable
52 # from the address specified in the FROM_ADDR
54 # Note that sendmail must be proper installed
56 send_email_report()
58 to=$TO_ADDR
59 from=$FROM_ADDR
60 subject=$1
61 contents=$2
62 tmpfile=/tmp/email.XXXXXXXXXX
63 emailfile=$(mktemp $tmpfile)
65 die_on_error $? "Could not create temporary e-mail file"
67 echo -e "To: $to\nFrom: $from" > $emailfile
68 echo -e "Subject: $subject" >> $emailfile
69 echo -e "Content-type: text/plain\n\n" >> $emailfile
70 echo -e "$contents\n." >> $emailfile
72 cat $emailfile | $SENDMAIL -t
73 ret=$?
75 rm -f $emailfile
77 return $ret