-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path.functions
67 lines (57 loc) · 2 KB
/
.functions
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
# print currently active shell
shell () {
ps -p $$ | tail -1 | awk '{print $NF}'
}
mcd () {
mkdir -p $@ && cd $1 && echo "Created directory \"$@\""
}
# calculate difference between two dates in days, hours and minutes
# order of parameters doesn't matter
datediff() {
if [ $# -lt 1 ]; then
echo "Computes a difference between the starting and ending date and time in days, hours and minutes."
echo "If the second argument is omitted, the current date and time is used as the ending date.\n"
echo "Usage: datediff <start> [end]\n"
echo 'datediff <hh>[:mm][:ss] <hh>[:mm][:ss]'
echo 'datediff "<YYYY-MM-DD> [hh][:mm][:ss]" "<YYYY-MM-DD> [hh][:mm][:ss]"'
return
fi
# use the current date and time if second argument is omitted
if [ $# -lt 2 ]; then
2=`date`
fi
# check if arguments are valid dates
d1=$(date -d "$1")
if [ $? -ne 0 ]; then
echo 'First argument is not a valid date'
return
fi
d2=$(date -d "$2")
if [ $? -ne 0 ]; then
echo 'Second argument is not a valid date'
return
fi
# display parsed and normalized dates
echo "Start: $d1"
echo "End: $d2"
echo
# convert input dates to unix timestamps
d1=$(date -d "$1" +%s)
d2=$(date -d "$2" +%s)
# calculate a difference in seconds
diff=$(( (d1 - d2) > 0 ? (d1 - d2) : (d2 - d1) ))
# format and print the difference
days=$(( diff / 86400 ))
hours=$(( (diff % 86400) / 3600 ))
hours_total=$(( diff / 3600 ))
minutes=$(( ((diff % 86400) % 3600) / 60 ))
seconds=$(( ((diff % 86400) % 3600) % 60 ))
echo "Difference: $days day(s) $hours hour(s) $minutes minute(s) $seconds seconds(s)"
echo " $hours_total hour(s) $minutes minute(s) $seconds seconds(s)"
echo " $diff second(s)"
}
# find <paramater> in stdin, highlight it and send it to stdout
highlight () {
# perl -pe "s/$1/\e[1;31;43m$&\e[0m/g"
perl -pe "s/$1/$fg[black]$bg[green]$&\e[0m/g"
}