-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwttr.in.sh
29 lines (27 loc) · 848 Bytes
/
wttr.in.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
#!/bin/bash
declare -A wttr_options
BASE_URL="wttr.in"
wttr_options=(
["full"]="/?nQ"
["today"]="/?1nQ"
["tomorrow"]="/?n2Q, 10"
)
wttr.in () {
if [ $# -eq 0 ]; then
curl -s "${BASE_URL}/?n2Q" | head -n -2
elif [ "$1" ] && [ ${wttr_options[$1]} ]; then
STRING=${wttr_options["$1"]};
IFS=#
fields=(${STRING//, /#}) # Be mindful of whitespaces!
URL=${fields[0]} # Extract (base) url
if [ "$1" == "full" ] || [ "$1" == "today" ]; then
curl -s "$BASE_URL$URL" | head -n -2
elif [ "$1" == "tomorrow" ]; then
LINES=${fields[1]} # Extract number of lines to omit
curl -s "$BASE_URL$URL" | head -n -2 | tail -n -$LINES
fi
else
echo "Option '$1' not recognized. Available options '${!wttr_options[@]}'"
fi
}
alias weather="wttr.in"