-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathweather
executable file
·125 lines (116 loc) · 3.08 KB
/
weather
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
#!/usr/bin/env bash
source weather.cfg
function syntax_error {
echo "Syntax error. Use -h or --help for options"
exit 2
}
function help_message {
echo "A bash wrapper for wttr.in."
echo "Displays weather forecast for a location, or the current phase of the moon."
echo "Options:"
echo " -0, --today: Display today's quadripartite weather forecast"
echo " -1, --tomorrow: Display tomorrow's"
echo " -2, --nextday: Display the next day's"
echo " -3, --three: Three-day quad"
echo " -c, --city CITY: Specify the location city as CITY, e.g. -c Berlin"
echo " -m, --moon: Show the moon phase (location agnostic)"
echo "Without any options, display today's simple forecast for your default location"
exit 4
}
OPTSPEC=":hmc0123-:"
while getopts "$OPTSPEC" optchar; do
case "${optchar}" in
-)
case "${OPTARG}" in
help)
help_message
;;
moon)
moon=true
;;
today)
today=true
;;
tomorrow)
tomorrow=true
;;
nextday)
nextday=true
;;
three)
three=true
;;
city)
nodisplaylocation=true
city="${!OPTIND}"
OPTIND="$(( OPTIND + 1 ))"
;;
*)
syntax_error
;;
esac
;;
h)
help_message
;;
m)
moon=true
;;
0)
today=true
;;
1)
tomorrow=true
;;
2)
nextday=true
;;
3)
three=true
;;
c)
nodisplaylocation=true
city="${!OPTIND}"
OPTIND="$(( OPTIND + 1 ))"
;;
*)
syntax_error
;;
esac
done
if [[ ( $today || $tomorrow || $nextday || $three = true ) && $moon = true ]]; then
echo "The weather and moon phase views are mutually exclusive; please choose one or the other"
exit 4
fi
# In awk processes below, xt and xh are lines to exclude from the head and tail of curl output, respectively
# Should be slightly more portable than using head and tail
# To extract tail, awk builds a buffer and begins printing it only after NR>xt e.g. a[1] is printed when a[xt] is being recorded
weather_data=$(curl -s http://wttr.in/$city 2> /dev/null)
function fmt_wttr {
# $1=xh, $2=xt e.g. to exclude 3 lines from head and 5 from tail: fmt_wttr 3 5
echo "$weather_data" | awk -v xt=$2 -v len=$3 '{if(NR>xt) print a[NR%xt]; a[NR%xt]=$0; i++; if (i>len) exit}' | awk -v xh=$1 'NR>xh{print $0}'
}
function fmt_moon {
curl -s http://wttr.in/moon 2> /dev/null | awk -v xt=3 '{if(NR>xt) print a[NR%xt]; a[NR%xt]=$0}'
}
if [[ $three = true ]]; then
fmt_wttr 0 2 39
elif [[ $today || $tomorrow || $nextday = true ]]; then
if [[ $today = true ]]; then
fmt_wttr 7 22 39
fi
if [[ $tomorrow = true ]]; then
fmt_wttr 17 12 39
fi
if [[ $nextday = true ]]; then
fmt_wttr 27 2 39
fi
elif [[ $moon = true ]]; then
fmt_moon
elif [[ $nodisplaylocation = true ]]; then
fmt_wttr 1 33 39
echo ""
else
curl -s http://wttr.in/$city 2> /dev/null | awk -v xt=33 '{if(NR>xt) print a[NR%xt]; a[NR%xt]=$0}'
echo ""
fi