-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTutorNotification.sh
More file actions
137 lines (113 loc) · 3.67 KB
/
TutorNotification.sh
File metadata and controls
137 lines (113 loc) · 3.67 KB
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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
#!/bin/bash
#let format be TutorNotification_{month}_{day}_{time}
WORKING_PATH=$PWD'/' #here enter you absolute path of the working path, e.g /home/user/Development/something/
#java things
HISTORY="History"
IMPORT_FILE="${WORKING_PATH}result.csv"
#functions
function DoImporting() {
echo "Starting Import" >> TutorLogger
UPDATE="FALSE"
if [ -f "${IMPORT_FILE}" ] ; then
echo "${IMPORT_FILE} exists" >> TutorLogger
LINE_NUM=$(wc --lines ${IMPORT_FILE} | cut -d' ' -f1)
if [ "${LINE_NUM}" -gt 1 ] ; then
UPDATE="TRUE"
echo "DoImporting true" >> TutorLogger
cp ${IMPORT_FILE} ${TEMP_FILE}
fi
else
echo "${IMPORT_FILE} not exist"
fi
if [ ${UPDATE} = "TRUE" ] ; then
return 0
else
return 1
fi
}
function DoDiffing() {
#diff the previous greped result
SEND="FALSE"
#get the last file name
LAST_FILE=$(ls -lt ${HISTORY}/TutorNotification_*_*_* | head -1 | rev | cut -d' ' -f1 | rev)
echo "The last file is: $LAST_FILE" >> TutorLogger
#if diff not blank, output the newest file and set SEND 'TRUE'
DIFF_RESULT=""
if [ "$LAST_FILE" != "" ] ; then
DIFF_RESULT=$(diff $TEMP_FILE $LAST_FILE)
echo $DIFF_RESULT >> TutorLogger
fi
if [ "$DIFF_RESULT" != "" ] || [ "$LAST_FILE" = "" ] ; then
SEND="TRUE"
#rename the tmpFile
F_MONTH=$(date -R | cut -d' ' -f3)
F_DAY=$(date -R | cut -d' ' -f2)
F_TIME=$(date -R | cut -d' ' -f5)
NEW_FILE="${WORKING_PATH}${HISTORY}/TutorNotification_${F_MONTH}_${F_DAY}_${F_TIME}"
mv $TEMP_FILE $NEW_FILE
TEMP_FILE="$NEW_FILE"
echo "TEMP_FILE new name: "$TEMP_FILE >> TutorLogger
fi
if [ ${SEND} = "TRUE" ] ; then
SendMail $NEW_FILE
return 0
else
#SendMail $TEMP_FILE #debug use only to check mailing and crontab
return 1
fi
}
function SendMail() { #input arg 1 is the file path
cp $1 "$1_backUp"
sed -i '1s/^/\n/' $1
sed -i '1s/^/Here is the update:\n/' $1
sed -i '1s/^/\n/' $1
sed -i '1s/^/Dear recipients,\n/' $1
echo "" >> $1
echo "Regards," >> $1
echo "$(whoami)" >> $1
F_TIME=$(date | cut -d' ' -f4)
for recipient in "${RECIPIENTS[@]}"
do
echo "[Looping] recipient is : ${recipient}"
cat $1 | mail -s "[TutorNotification] : $TODAY, $F_TIME" "${recipient}"
done
rm $1
mv "$1_backUp" $1
}
#=======================================================================================main part============================================================================
pushd ${WORKING_PATH}
echo "======================Debug Log Start :" >> TutorLogger
echo "Now the working path is:" >> TutorLogger
pwd >> TutorLogger
date >> TutorLogger
TEMP_FILE="${WORKING_PATH}tmpFile"
RECIPIENTS=($(tail -n+2 ${WORKING_PATH}config.csv | /usr/local/bin/awk-csv-parser --output-separator=',' | grep WC_RECIPIENT | awk -F"," '{print $3}'))
echo "[Testing] , working path is: ${WORKING_PATH}" >> TutorLogger
echo "[Testing] , recipients are: ${RECIPIENTS[*]}" >> TutorLogger
ant >> TutorLogger
if [ ! -d "${WORKINGPATH}${HISTORY}" ] ; then
echo "History folder not exists, going to create it..." >> TutorLogger
mkdir ${HISTORY}
fi
#confirm today date
#TODAY=$(date | cut -d' ' -f2-3)
#TODAY=$(date -R --date='-1 day'| cut -d' ' -f2-3 )
TODAY=$(date -R | cut -d' ' -f2-3 )
echo "TODAY is "${TODAY} >> TutorLogger
echo "TEMP_FILE is "$TEMP_FILE >> TutorLogger
#clear temp file
if [[ -e "$TEMP_FILE" ]] ; then
echo "Deleting $TEMP_FILE ..."
rm "$TEMP_FILE"
fi
if DoImporting ; then
echo "DoImporting success" >> TutorLogger
if DoDiffing ; then
echo "DoDiffing sucess" >> TutorLogger
else
echo "DoDiffing no result" >> TutorLogger
fi
else
echo "DoImporting no result" >> TutorLogger
fi
popd