-
Notifications
You must be signed in to change notification settings - Fork 0
/
age
executable file
·53 lines (42 loc) · 837 Bytes
/
age
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
#!/usr/bin/env bash
function check_opts() {
while getopts ":b:" opt; do
case $opt in
b)
BIRTH=$OPTARG;
echo $BIRTH > ~/.age/birth;
;;
\?)
echo "Invalid option: -$OPTARG" >&2
exit 1
;;
:)
echo "Option -$OPTARG requires an argument." >&2
exit 1
;;
esac
done
shift $((OPTIND -1))
}
function get_birth() {
if [[ -e ~/.age/birth ]]; then
let BIRTH=$(<~/.age/birth)
else
let BIRTH=19710311
fi
}
function calculate_birth() {
let NOW=`date +%Y%m%d`
let DIFF=(`date +%s -d $NOW`-`date +%s -d $BIRTH`)/86400
}
function user_output() {
bold=$(tput bold)
echo -e "You are \e[40m\033[1m\e[93m$DIFF\033[0m days old."
}
function main() {
check_opts "$@"
get_birth
calculate_birth
user_output
}
main "$@"