-
Notifications
You must be signed in to change notification settings - Fork 1
/
update-defaulter
executable file
·56 lines (36 loc) · 1.59 KB
/
update-defaulter
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
#!/bin/bash
set -x
location=$(dirname "$HOME")
{ [[ -n "$1" ]] && ! [[ "$1" == '--help' ]]; } && limit=$(date +%s -d "$1") || {
echo "Usage: updateDefaulter yymmdd"
echo "defaulter -> anyone who has not paid before given date."
exit 1;
}
tempfile=$(mktemp)
# Get a list of all fees.txt files
readarray -t feefiles < <(find "$location" -name 'fees.txt' -type f)
# List each file with its most recent payment timestamp in the temp file
# If timestamp is not before given date, do not add to list; instead, add
# student's rollno. to feeDefaulters.txt
for path in "${feefiles[@]}"; do
# Get a UNIX timestamp of the most recent transaction recorded in the file
timestamp=$(tac "$path" | grep 'Fee payment on' | cut -f4 -d' ' | xargs date +%s -d)
# Get the student's userDetails.txt
userfile=$(dirname $(dirname "$path"))/userDetails.txt
# Get name of student
name=$(sed -nE 's/Name: (.*)/\1/p' "$userfile")
# Get rollno of student
rollno=$(grep -o -E '[0-9]{9}' "$userfile")
if [[ -z $timestamp ]] || [[ $timestamp -gt $limit ]]; then # student is defaulter
printf '%s %s\n' "$name" "$rollno" >> "$location"/feeDefaulters.txt
else # add to the list
printf '%s %s %s\n' "$timestamp" "$name" "$rollno" >> "$tempfile"
fi
done
# Sort the list
sort -k1,1 -n -o "$tempfile"{,}
# Add top 5 students to announcements.txt
readarray -n 5 lines < <(cat "$tempfile" | cut -f2,3 -d' ')
echo 'The following are the first 5 students to pay fees:' > "$location"/announcements.txt
printf '%s\n' "${lines[@]}" >> "$location"/announcements.txt
rm -f "$tempfile"