-
Notifications
You must be signed in to change notification settings - Fork 0
/
when_did_I_get_here.sh
executable file
·72 lines (48 loc) · 1.66 KB
/
when_did_I_get_here.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
71
#!/usr/bin/env bash
# A lightweight tool for telling you how long you've been at work
# Authored by David Browning and Jack Kiefer
month=$(date | tr -s ' ' | cut -d' ' -f2)
day=$(date | tr -s ' ' | cut -d' ' -f3)
curDate="$month $day"
echo "It is $curDate"
echo ""
# Get the time the machine booted this morning
arrivalTime=$(last -w $USER | tr -s ' ' | grep "$curDate" | tail -n1 | tr -s ' ' | cut -d' ' -f7)
TIME1="$arrivalTime:00"
# Get the current time
TIME2=$(date +"%T")
# Convert the times to seconds from the Epoch
SEC1=`date +%s -d ${TIME1}`
SEC2=`date +%s -d ${TIME2}`
# Use expr to do the math, let's say TIME1 was the start and TIME2 was the finish
DIFFSEC=`expr ${SEC2} - ${SEC1}`
echo The first recorded login today was at ${TIME1}
echo It is currently ${TIME2}
# And use date to convert the seconds back to something more meaningful
elapsed=$(date +%H:%M:%S -ud @${DIFFSEC})
# Make it pretty
h=$(echo $elapsed | cut -d':' -f 1)
m=$(echo $elapsed | cut -d':' -f 2)
s=$(echo $elapsed | cut -d':' -f 3)
echo It has been $h hours, $m minutes, and $s seconds
if ! [ -z "$1" ]
then
TOSECDEC=$(echo "${1} * 3600" | bc)
TOSEC=$(echo $TOSECDEC | cut -f1 -d".")
LEAVESEC=`expr ${SEC1} + ${TOSEC}`
if [ $SEC2 -gt $LEAVESEC ]
then
echo ""
echo "You have already met your goal of $1 hours!!"
exit
fi
SECTOGO=`expr ${LEAVESEC} - ${SEC2}`
togo=$(date +%H:%M:%S -ud @${SECTOGO})
th=$(echo $togo | cut -d':' -f 1)
tm=$(echo $togo | cut -d':' -f 2)
ts=$(echo $togo | cut -d':' -f 3)
echo ""
echo To reach your target of $1 hours you have
echo $th hours, $tm minutes, and $ts seconds to go
fi
exit