-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathforecast
executable file
·77 lines (66 loc) · 2.17 KB
/
forecast
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
#!/bin/bash
# Required Optional
# Use: forecast <location> <units>
# Importing Prefs.cfg #
source Prefs.cfg
# First parameter after command #
location=$(echo $1 | sed 's/\-/%20/g')
# Optional parameters after required parameter #
for i in {2..3}
do
eval temp='$'$i
if [[ ! -z $temp ]]
then
case $temp in
"-c")
clear
;;
"-i")
units="imperial"
;;
"-m")
units="metric"
;;
esac
fi
done
# Time format selection #
if [[ "$units" == "imperial" ]]
then
unitTemp="F"
unitWind="mph"
else
unitTemp="C"
unitWind="m/s"
fi
# array used as text to put before weather conditions
weatherText=("-" "- The temperature will be around " "$unitTemp, with a minimum of " "$unitTemp, and a maximum of " "$unitTemp. Weather of the day: " ". The wind velocity will be around " "$unitWind. Finally, the chance of raining in this day is ")
read vWeatherFull < <(echo $(curl -s --request GET --url 'api.openweathermap.org/data/2.5/forecast?q='$location'&appid='$apiKey'&units='$units | jq '.list[] | "\(.dt)-\(.main.temp)-\(.main.temp_min)-\(.main.temp_max)-\(.weather[].main)-\(.wind.speed)-\(.pop)"'))
# This line serves to remove every quotation mark
vWeatherFull=$(echo $vWeatherFull | tr -d '"')
# Atm vWeatherFull contains all data in one variable, this line break down vWeatherFull in an array, every cell of the array is made by: time-temp-temp_min-temp_max-weather-wind-possibilityOfPrecipitation (this is one variable, it will become its own array in the next loop)#
IFS=' ' read -r -a aWeatherFull <<< "$vWeatherFull"
# Most important loop! Takes the main array and break it down in smaller arrays dynamically created #
for (( i = 0; i < ${#aWeatherFull[*]}; i++ ))
do
IFS='-' read -r -a aWeatherPoint$i <<< "${aWeatherFull[i]}"
done
# This loop writes weather for every three hours
for (( i = 0; i < ${#aWeatherFull[*]}; i++ ))
do
eval time='$'"{aWeatherPoint"$i"[0]}"
time=$(date -d @$time +%H)
if [ $time == 12 ] || [ $time == 13 ] || [ $time == 14 ]
then
for (( j=0; j<7;j++ ))
do
eval var='$'"{aWeatherPoint"$i"["$j"]}"
if [[ $j == 0 ]]
then
var=$(date -d @$var '+%A %d %B %Y')
fi
echo -n ${weatherText[$j]}$var
done
echo -e "\n"
fi
done