-
Notifications
You must be signed in to change notification settings - Fork 1
/
.cmd_functions.sh
executable file
·71 lines (64 loc) · 1.72 KB
/
.cmd_functions.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
#/usr/bin/env bash
## Command wrapper functions
##
## Chain functions together in a selective order and prefix almost any bash command. Use
## these to add a log file, easy date printing (for timing), trace (like a single-command
## xtrace output), and email notices.
##
## .cmd_functions, ctime, ctrace, cnotify, clog
##
## Variables:
## cret = the return value of the last command (initialize: export cret)
## cerror = first error code of any command (initialize: export cerror=0)
##
## ctime, prints date and time before and after command execution
function ctime {
date
"$@"
cret=$?
[[ "$cerror" -eq 0 && $cret -ne 0 ]] && cerror=$cret
date
return $cret
}
## ctrace, shows the exit value
function ctrace {
echo "+ $@"
"$@"
cret=$?
[[ "$cerror" -eq 0 && $cret -ne 0 ]] && cerror=$cret
echo "EXIT $cret"
return $cret
}
## crun, combins ctime and ctrace
function crun {
ctime ctrace "$@"
}
## clog, append to a log file. std err and out are combined and printed to the console
function clog {
LOG="${1?log file}"
shift 1
# >(Process substitution) must be used in place of pipe so $cmd will run
# in this shell giving access to the return code $?.
"$@" > >(tee -a "$LOG") 2>&1
cret=$?
[[ "$cerror" -eq 0 && $cret -ne 0 ]] && cerror=$cret
return $cret
}
## Depends on sendmail
## USAGE: cnotify [email protected] "subject" echo Output from any command and parameters
function cnotify {
EMAIL=${1?email}
SUBJECT=${2?subject}
shift 2
cnotify_tmp=$(mktemp "/tmp/cnotify_tmp.XXXXXXX")
"$@" > $cnotify_tmp
cret=$?
[[ "$cerror" -eq 0 && $cret -ne 0 ]] && cerror=$cret
(
echo "Subject: ($cret) $SUBJECT"
echo
cat $cnotify_tmp
)|/usr/sbin/sendmail $EMAIL
rm $cnotify_tmp
return $cret
}