Sunrise & Sunset - simple script is for sunrise and sunset times for any location on the Linux command line.
#! / Bin / bash
1. First obtain a location code from: https://weather.codes/search/
2. Insert your location. For example LOXX0001 is a location code for Bratislava, Slovakia location = "LOXX0001"
3. Obtain sunrise and sunset raw data from weather.com sun_times = $ (lynx --dump https://weather.com/weather/today/l/$location | grep "\ * Sun" | sed "s / [[: alpha:]] // g; s / * // ") # Extract sunrise and sunset times and convert to 24 hour format sunrise = $ (date --date =" `echo $ sun_times | awk '{print $ 1}'` AM "+% R) sunset = $ (date --date =" `echo $ sun_times | awk '{print $ 2}'` PM "+% R)
4. Use $ sunrise and $ sunset variables to fit your needs. Example: echo "Sunrise for location $ location: $ sunrise" echo "Sunset for location $ location: $ sunset"
Alternatively, you can also clone the latest version from github:
$ Git clone https://github.com/linuxconfig/Sunrise-Sunset-Shell-Script.git
Get the location code https://weather.codes/search/ and assign it a variable while replacing it. Save the file and execute it:
$ Chmod + x sunrise-sunset.sh
Make sure lynx is available on the Linux system and run:
UBUNTU / DEBIAN # apt install lynx CENTOS / REDHAT # yum install lynx
Then run the script:
$ ./sunrise-sunset.sh Sunrise for location LOXX0001: 06:47 Sunset for location LOXX0001: 18:34