Giuseppe Parrello

 

Bash - Come recuperare gli orari di alba e tramonto


In questa pagina fornisco il codice script per alcuni servizi meteo allo scopo di recuperare i tempi di alba e tramonto, eventualmente usandoli per impostare uno o due script di cron. Si prega di prendere nota dei seguenti punti:

www.datameteo.com

#!/bin/bash

#
# We retrieve sunrise and sunset times from www.datameteo.com
#
# 1) go to address http://www.datameteo.com/meteo/
# 2) insert your location inside field "Search a place"
# 3) copy and paste the internet address here in this script
#

url="http://www.datameteo.com/meteo/weather_Milan"

# we get all the web page
allfile=$( wget -qO-  $url | sed 's/<br/\n/g' | sed 's/<\/span>/\n/g' | grep -i ">Sunrise");

# we get the original sunrise/sunset time values
sunset_str=$(echo $allfile | cut -d ":" -f 2,3 | sed 's|[^0-9]*\([0-9\:]*\)|\1 |g')
sunrise_str=$(echo $allfile | cut -d ":" -f 4,5 | sed 's|[^0-9]*\([0-9\:]*\)|\1 |g')
echo $sunset_str
echo $sunrise_str

# we convert time values to Time in 24-hour format
sunset=$(/opt/bin/date --date="$sunset_str" +%R)
sunrise=$(/opt/bin/date --date="$sunrise_str" +%R)
echo $sunset
echo $sunrise

# we get hour/min values for sunset
sun_set1=$(echo $sunset | cut -d ":" -f 1)
sun_set2=$(echo $sunset | cut -d ":" -f 2)
echo $sun_set1
echo $sun_set2

# we get hour/min values for sunrise
sunrise1=$(echo $sunrise | cut -d ":" -f 1)
sunrise2=$(echo $sunrise | cut -d ":" -f 2)
echo $sunrise1
echo $sunrise2

# uncomment if you want to delete two cron rows for sunset and sunrise scripts
#cru d sunsetscript
#cru d sunrisescript

# uncomment if you want to add two cron rows for sunset and sunrise scripts
#cru a sunsetscript  "$sun_set2 $sun_set1 * * * sh /opt/etc/myscripts/sunsetscript.sh"
#cru a sunrisescript "$sunrise2 $sunrise1 * * * sh /opt/etc/myscripts/sunrisescript.sh"

# crontab values
#
# .---------------- [m]inute: (0 - 59)
# |  .------------- [h]our: (0 - 23)
# |  |  .---------- [d]ay of month: (1 - 31)
# |  |  |  .------- [mon]th: (1 - 12) OR jan,feb,mar,apr...
# |  |  |  |  .---- [w]eek day: (0 - 6) (sunday=0 or 7)  OR sun,mon,tue,wed,thu,fri,sat
# |  |  |  |  |
#
# *  *  *  *  *  command to be executed
#

 

www.eurometeo.com

#!/bin/bash

#
# We retrieve sunrise and sunset times from www.eurometeo.com
#
# 1) go to address http://www.eurometeo.com/english/search
# 2) insert your location
# 3) copy and paste the internet address here in this script
#

url="http://www.eurometeo.com/english/forecast/city_LIML/weather/milan.htm"

# we get all the web page
allfile=$( wget -qO-  $url);

# we get the original sunrise/sunset time values
sunset_str=$(echo $allfile | sed 's/<br/\n/g' | sed 's/<\/br>/\n/g' | grep -i "The sun rises" | cut -d ">" -f 5 | cut -d "<" -f 1)
sunrise_str=$(echo $allfile | sed 's/<br/\n/g' | sed 's/<\/br>/\n/g' | grep -i "The sun rises" | cut -d ">" -f 7 | cut -d "<" -f 1)
echo $sunset_str
echo $sunrise_str

# we convert time values to Time in 24-hour format
sunset=$(/opt/bin/date --date="$sunset_str" +%R)
sunrise=$(/opt/bin/date --date="$sunrise_str" +%R)
echo $sunset
echo $sunrise

# we get hour/min values for sunset
sun_set1=$(echo $sunset | cut -d ":" -f 1)
sun_set2=$(echo $sunset | cut -d ":" -f 2)
echo $sun_set1
echo $sun_set2

# we get hour/min values for sunrise
sunrise1=$(echo $sunrise | cut -d ":" -f 1)
sunrise2=$(echo $sunrise | cut -d ":" -f 2)
echo $sunrise1
echo $sunrise2

# uncomment if you want to delete two cron rows for sunset and sunrise scripts
#cru d sunsetscript
#cru d sunrisescript

# uncomment if you want to add two cron rows for sunset and sunrise scripts
#cru a sunsetscript  "$sun_set2 $sun_set1 * * * sh /opt/etc/myscripts/sunsetscript.sh"
#cru a sunrisescript "$sunrise2 $sunrise1 * * * sh /opt/etc/myscripts/sunrisescript.sh"

# crontab values
#
# .---------------- [m]inute: (0 - 59)
# |  .------------- [h]our: (0 - 23)
# |  |  .---------- [d]ay of month: (1 - 31)
# |  |  |  .------- [mon]th: (1 - 12) OR jan,feb,mar,apr...
# |  |  |  |  .---- [w]eek day: (0 - 6) (sunday=0 or 7)  OR sun,mon,tue,wed,thu,fri,sat
# |  |  |  |  |
#
# *  *  *  *  *  command to be executed
#

 

www.ilmeteo.it

#!/bin/bash

# Only for italian users
#
# We retrieve sunrise and sunset times from ilmeteo.it
#
# 1) go to address https://www.ilmeteo.it
# 2) insert your location
# 3) copy and paste the internet address here in this script
#

url="https://www.ilmeteo.it/meteo/Milano"

# we get all the web page
allfile=$( wget -qO-  $url | sed 's/<script/\n/g' | sed 's/<\/script>/\n/g'  | grep -E "bi-eff-sole" | tr -d '\n');

# we get the original sunrise/sunset time values
sunset_str=$(echo $allfile | cut -d " " -f 3 | cut -d "," -f 1)
sunrise_str=$(echo $allfile | cut -d " " -f 5 | cut -d "'" -f 1)
echo $sunset_str
echo $sunrise_str

# we convert time values to Time in 24-hour format
sunset=$(/opt/bin/date --date="$sunset_str" +%R)
sunrise=$(/opt/bin/date --date="$sunrise_str" +%R)
echo $sunset
echo $sunrise

# we get hour/min values for sunset
sun_set1=$(echo $sunset | cut -d ":" -f 1)
sun_set2=$(echo $sunset | cut -d ":" -f 2)
echo $sun_set1
echo $sun_set2

# we get hour/min values for sunrise
sunrise1=$(echo $sunrise | cut -d ":" -f 1)
sunrise2=$(echo $sunrise | cut -d ":" -f 2)
echo $sunrise1
echo $sunrise2

# uncomment if you want to delete two cron rows for sunset and sunrise scripts
#cru d sunsetscript
#cru d sunrisescript

# uncomment if you want to add two cron rows for sunset and sunrise scripts
#cru a sunsetscript  "$sun_set2 $sun_set1 * * * sh /opt/etc/myscripts/sunsetscript.sh"
#cru a sunrisescript "$sunrise2 $sunrise1 * * * sh /opt/etc/myscripts/sunrisescript.sh"

# crontab values
#
# .---------------- [m]inute: (0 - 59)
# |  .------------- [h]our: (0 - 23)
# |  |  .---------- [d]ay of month: (1 - 31)
# |  |  |  .------- [mon]th: (1 - 12) OR jan,feb,mar,apr...
# |  |  |  |  .---- [w]eek day: (0 - 6) (sunday=0 or 7)  OR sun,mon,tue,wed,thu,fri,sat
# |  |  |  |  |
#
# *  *  *  *  *  command to be executed
#

 

en.sat24.com

#!/bin/bash

#
# We retrieve sunrise and sunset times from en.sat24.com
#
# 1) go to address https://en.sat24.com
# 2) insert your location
# 3) select tab "Today"
# 4) copy and paste the internet address here in this script
#

url="https://en.sat24.com/en/forecast/t/2876761/milano"

# we get all the web page
allfile=$( wget -qO-  $url);

# we get the original sunrise/sunset time values
sunset_str=$(echo $allfile | sed 's/<span/\n/g' | sed 's/<\/span>/\n/g' | grep -i "sunrise" | cut -d ">" -f 2)
sunrise_str=$(echo $allfile | sed 's/<span/\n/g' | sed 's/<\/span>/\n/g' | grep -i "sunset" | cut -d ">" -f 2)
echo $sunset_str
echo $sunrise_str

# we convert time values to Time in 24-hour format
sunset=$(/opt/bin/date --date="$sunset_str" +%R)
sunrise=$(/opt/bin/date --date="$sunrise_str" +%R)
echo $sunset
echo $sunrise

# we get hour/min values for sunset
sun_set1=$(echo $sunset | cut -d ":" -f 1)
sun_set2=$(echo $sunset | cut -d ":" -f 2)
echo $sun_set1
echo $sun_set2

# we get hour/min values for sunrise
sunrise1=$(echo $sunrise | cut -d ":" -f 1)
sunrise2=$(echo $sunrise | cut -d ":" -f 2)
echo $sunrise1
echo $sunrise2

# uncomment if you want to delete two cron rows for sunset and sunrise scripts
#cru d sunsetscript
#cru d sunrisescript

# uncomment if you want to add two cron rows for sunset and sunrise scripts
#cru a sunsetscript  "$sun_set2 $sun_set1 * * * sh /opt/etc/myscripts/sunsetscript.sh"
#cru a sunrisescript "$sunrise2 $sunrise1 * * * sh /opt/etc/myscripts/sunrisescript.sh"

# crontab values
#
# .---------------- [m]inute: (0 - 59)
# |  .------------- [h]our: (0 - 23)
# |  |  .---------- [d]ay of month: (1 - 31)
# |  |  |  .------- [mon]th: (1 - 12) OR jan,feb,mar,apr...
# |  |  |  |  .---- [w]eek day: (0 - 6) (sunday=0 or 7)  OR sun,mon,tue,wed,thu,fri,sat
# |  |  |  |  |
#
# *  *  *  *  *  command to be executed
#

 

www.timeanddate.com

#!/bin/bash

#
# We retrieve sunrise and sunset times from www.timeanddate.com
#
# 1) go to address https://www.timeanddate.com/
# 2) insert your location inside field "World Clock"
# 3) go to field "Sun & Moon" and select "Sun & Moon Today"
# 4) copy and paste the internet address here in this script
#

url="https://www.timeanddate.com/astronomy/italy/milan"

# we get all the web page
allfile=$( wget -qO-  $url);

# we get the original sunrise/sunset time values
sunset_str=$(echo $allfile | sed 's/<p/\n/g' | grep -i "Sunrise Today" | cut -d ">" -f 5 | cut -d "<" -f 1 | tr "." :)
sunrise_str=$(echo $allfile | sed 's/<p/\n/g' | grep -i "Sunset Today" | cut -d ">" -f 12 | cut -d "<" -f 1 | tr "." :)
echo $sunset_str
echo $sunrise_str

# we convert time values to Time in 24-hour format
sunset=$(/opt/bin/date --date="$sunset_str" +%R)
sunrise=$(/opt/bin/date --date="$sunrise_str" +%R)
echo $sunset
echo $sunrise

# we get hour/min values for sunset
sun_set1=$(echo $sunset | cut -d ":" -f 1)
sun_set2=$(echo $sunset | cut -d ":" -f 2)
echo $sun_set1
echo $sun_set2

# we get hour/min values for sunrise
sunrise1=$(echo $sunrise | cut -d ":" -f 1)
sunrise2=$(echo $sunrise | cut -d ":" -f 2)
echo $sunrise1
echo $sunrise2

# uncomment if you want to delete two cron rows for sunset and sunrise scripts
#cru d sunsetscript
#cru d sunrisescript

# uncomment if you want to add two cron rows for sunset and sunrise scripts
#cru a sunsetscript  "$sun_set2 $sun_set1 * * * sh /opt/etc/myscripts/sunsetscript.sh"
#cru a sunrisescript "$sunrise2 $sunrise1 * * * sh /opt/etc/myscripts/sunrisescript.sh"

# crontab values
#
# .---------------- [m]inute: (0 - 59)
# |  .------------- [h]our: (0 - 23)
# |  |  .---------- [d]ay of month: (1 - 31)
# |  |  |  .------- [mon]th: (1 - 12) OR jan,feb,mar,apr...
# |  |  |  |  .---- [w]eek day: (0 - 6) (sunday=0 or 7)  OR sun,mon,tue,wed,thu,fri,sat
# |  |  |  |  |
#
# *  *  *  *  *  command to be executed
#

 

weather.com

#!/bin/bash

#
# We retrieve sunrise and sunset times from weather.com
#
# 1) go to address https://weather.codes/search/
# 2) insert your location
# 3) copy and paste the output location code here in this script
#

location="ITXX0042"

# we get all the web page
allfile=$( wget -qO-  https://weather.com/weather/today/l/$location);

# we get the original sunrise/sunset time values
sunset_str=$(echo $allfile | sed 's/<span/\n/g' | sed 's/<\/span>/\n/g' | grep -E "dp0-details-sunset" | cut -d ">" -f 2)
sunrise_str=$(echo $allfile | sed 's/<span/\n/g' | sed 's/<\/span>/\n/g' | grep -E "dp0-details-sunrise" | cut -d ">" -f 2)
echo $sunset_str
echo $sunrise_str

# we convert time values to Time in 24-hour format
sunset=$(/opt/bin/date --date="$sunset_str" +%R)
sunrise=$(/opt/bin/date --date="$sunrise_str" +%R)
echo $sunset
echo $sunrise

# we get hour/min values for sunset
sun_set1=$(echo $sunset | cut -d ":" -f 1)
sun_set2=$(echo $sunset | cut -d ":" -f 2)
echo $sun_set1
echo $sun_set2

# we get hour/min values for sunrise
sunrise1=$(echo $sunrise | cut -d ":" -f 1)
sunrise2=$(echo $sunrise | cut -d ":" -f 2)
echo $sunrise1
echo $sunrise2

# uncomment if you want to delete two cron rows for sunset and sunrise scripts
#cru d sunsetscript
#cru d sunrisescript

# uncomment if you want to add two cron rows for sunset and sunrise scripts
#cru a sunsetscript  "$sun_set2 $sun_set1 * * * sh /opt/etc/myscripts/sunsetscript.sh"
#cru a sunrisescript "$sunrise2 $sunrise1 * * * sh /opt/etc/myscripts/sunrisescript.sh"

# crontab values
#
# .---------------- [m]inute: (0 - 59)
# |  .------------- [h]our: (0 - 23)
# |  |  .---------- [d]ay of month: (1 - 31)
# |  |  |  .------- [mon]th: (1 - 12) OR jan,feb,mar,apr...
# |  |  |  |  .---- [w]eek day: (0 - 6) (sunday=0 or 7)  OR sun,mon,tue,wed,thu,fri,sat
# |  |  |  |  |
#
# *  *  *  *  *  command to be executed
#

 

www.weather-forecast.com

#!/bin/bash

#
# We retrieve sunrise and sunset times from www.weather-forecast.com
#
# 1) go to address https://www.weather-forecast.com
# 2) insert your location
# 3) copy and paste the internet address here in this script
#

url="https://www.weather-forecast.com/locations/Milan/forecasts/latest"

# we get all the web page
allfile=$( wget -qO-  $url);

# we get the original sunrise/sunset time values
sunset_str=$(echo $allfile | sed 's/<img/\n/g' | sed 's/<\/span>/\n/g' | grep -E "b-forecast__table-value" | grep -i "sunrise" | cut -d ">" -f 7)
sunrise_str=$(echo $allfile | sed 's/<img/\n/g' | sed 's/<\/tr>/\n/g' | grep -E "b-forecast__table-value" | grep -i "sunset" | cut -d ">" -f 11 | cut -d "<" -f 1)
echo $sunset_str
echo $sunrise_str

# we convert time values to Time in 24-hour format
sunset=$(/opt/bin/date --date="$sunset_str" +%R)
sunrise=$(/opt/bin/date --date="$sunrise_str pm" +%R)
echo $sunset
echo $sunrise

# we get hour/min values for sunset
sun_set1=$(echo $sunset | cut -d ":" -f 1)
sun_set2=$(echo $sunset | cut -d ":" -f 2)
echo $sun_set1
echo $sun_set2

# we get hour/min values for sunrise
sunrise1=$(echo $sunrise | cut -d ":" -f 1)
sunrise2=$(echo $sunrise | cut -d ":" -f 2)
echo $sunrise1
echo $sunrise2

# uncomment if you want to delete two cron rows for sunset and sunrise scripts
#cru d sunsetscript
#cru d sunrisescript

# uncomment if you want to add two cron rows for sunset and sunrise scripts
#cru a sunsetscript  "$sun_set2 $sun_set1 * * * sh /opt/etc/myscripts/sunsetscript.sh"
#cru a sunrisescript "$sunrise2 $sunrise1 * * * sh /opt/etc/myscripts/sunrisescript.sh"

# crontab values
#
# .---------------- [m]inute: (0 - 59)
# |  .------------- [h]our: (0 - 23)
# |  |  .---------- [d]ay of month: (1 - 31)
# |  |  |  .------- [mon]th: (1 - 12) OR jan,feb,mar,apr...
# |  |  |  |  .---- [w]eek day: (0 - 6) (sunday=0 or 7)  OR sun,mon,tue,wed,thu,fri,sat
# |  |  |  |  |
#
# *  *  *  *  *  command to be executed
#

 

www.wunderground.com

#!/bin/bash

#
# We retrieve sunrise and sunset times from www.wunderground.com
#
# 1) go to address https://www.wunderground.com/
# 2) insert your location
# 3) press tab "HOURLY"
# 3) copy and paste the internet address here in this script
#

url="https://www.wunderground.com/hourly/it/milan?cm_ven=localwx_hour"

# we get all the web page
allfile=$( wget -qO-  $url);

# we get the original sunrise/sunset time values
sunset_str=$(echo $allfile |  tr -d '\n' | sed 's/<div/\n/g' | sed 's/<\/div>/\n/g'  | grep -E "astro-data sunrise-icon")
sunrise_str=$(echo $allfile |  tr -d '\n' | sed 's/<div/\n/g' | sed 's/<\/div>/\n/g'  | grep -E "astro-data sunset-icon")
echo $sunset_str
echo $sunrise_str

# we get time values and am/pm values
sunset_time1=$(echo $sunset_str | cut -d ">" -f 3 | cut -d "<" -f 1)
sunset_time2=$(echo $sunset_str | cut -d ">" -f 5 | cut -d "<" -f 1)
sunrise_time1=$(echo $sunrise_str | cut -d ">" -f 3 | cut -d "<" -f 1)
sunrise_time2=$(echo $sunrise_str | cut -d ">" -f 5 | cut -d "<" -f 1)

# we convert time values to Time in 24-hour format
sunset=$(/opt/bin/date --date="$sunset_time1 $sunset_time2" +%R)
sunrise=$(/opt/bin/date --date="$sunrise_time1 $sunrise_time2" +%R)
echo $sunset
echo $sunrise

# we get hour/min values for sunset
sun_set1=$(echo $sunset | cut -d ":" -f 1)
sun_set2=$(echo $sunset | cut -d ":" -f 2)
echo $sun_set1
echo $sun_set2

# we get hour/min values for sunrise
sunrise1=$(echo $sunrise | cut -d ":" -f 1)
sunrise2=$(echo $sunrise | cut -d ":" -f 2)
echo $sunrise1
echo $sunrise2

# uncomment if you want to delete two cron rows for sunset and sunrise scripts
#cru d sunsetscript
#cru d sunrisescript

# uncomment if you want to add two cron rows for sunset and sunrise scripts
#cru a sunsetscript  "$sun_set2 $sun_set1 * * * sh /opt/etc/myscripts/sunsetscript.sh"
#cru a sunrisescript "$sunrise2 $sunrise1 * * * sh /opt/etc/myscripts/sunrisescript.sh"

# crontab values
#
# .---------------- [m]inute: (0 - 59)
# |  .------------- [h]our: (0 - 23)
# |  |  .---------- [d]ay of month: (1 - 31)
# |  |  |  .------- [mon]th: (1 - 12) OR jan,feb,mar,apr...
# |  |  |  |  .---- [w]eek day: (0 - 6) (sunday=0 or 7)  OR sun,mon,tue,wed,thu,fri,sat
# |  |  |  |  |
#
# *  *  *  *  *  command to be executed
#