Bash scripting 20a – weather

Another useful program is ANSI weather. We can use this to get a weather forecast or current weather conditions for a specified location, for example

This can be installed with

sudo apt install ansiweather
ansiweather -l torquay
 Weather in Torquay: 11 °C - UVI: 4.19 - Wind: 5.81 

By default, the output uses ansicolour, and as per man page this can be turned off by using -l

ansiweather -a false

Which is useful., as we have been looking at loops and generating data we can do combine some of this and generate current date / time and then the output of the weather forecast

echo "date" "data" # for reference, not written to file 
for ((i = 0 ; i < 10 ; i++)); do
    echo $(date), $(ansiweather -l Paignton -a false) # echo to screen (stdout)
    echo $(date), $(ansiweather -l Paignton -a false) >> weather.csv
done

#sources of help

#https://stackoverflow.com/questions/40175868/how-to-create-csv-file-using-shell-script

#https://stackoverflow.com/questions/1194882/how-to-generate-random-number-in-bash

#https://linuxhandbook.com/bash-loops/

#https://stackoverflow.com/questions/25242626/bash-redirect-output-to-log-file-with-day-date-in-the-name

Which produces

date data
Sat 19 Apr 20:52:54 BST 2025, Weather in Paignton: 11 °C - UVI: 4.19 - Wind: 5.52 m/s NE - Humidity: 79% - Pressure: 1006 hPa
Sat 19 Apr 20:52:55 BST 2025, Weather in Paignton: 11 °C - UVI: 4.19 - Wind: 5.52 m/s NE - Humidity: 79% - Pressure: 1006 hPa
Sat 19 Apr 20:52:56 BST 2025, Weather in Paignton: 11 °C - UVI: 4.19 - Wind: 5.52 m/s NE - Humidity: 79% - Pressure: 1006 hPa
Sat 19 Apr 20:52:56 BST 2025, Weather in Paignton: 11 °C - UVI: 4.19 - Wind: 5.52 m/s NE - Humidity: 79% - Pressure: 1006 hPa
Sat 19 Apr 20:52:57 BST 2025, Weather in Paignton: 11 °C - UVI: 4.19 - Wind: 5.52 m/s NE - Humidity: 79% - Pressure: 1006 hPa
Sat 19 Apr 20:52:58 BST 2025, Weather in Paignton: 11 °C - UVI: 4.19 - Wind: 5.52 m/s NE - Humidity: 79% - Pressure: 1006 hPa
Sat 19 Apr 20:52:59 BST 2025, Weather in Paignton: 11 °C - UVI: 4.19 - Wind: 5.52 m/s NE - Humidity: 79% - Pressure: 1006 hPa
Sat 19 Apr 20:52:59 BST 2025, Weather in Paignton: 11 °C - UVI: 4.19 - Wind: 5.52 m/s NE - Humidity: 79% - Pressure: 1006 hPa
Sat 19 Apr 20:53:00 BST 2025, Weather in Paignton: 11 °C - UVI: 4.19 - Wind: 5.52 m/s NE - Humidity: 79% - Pressure: 1006 hPa
Sat 19 Apr 20:53:01 BST 2025, Weather in Paignton: 11 °C - UVI: 4.19 - Wind: 5.52 m/s NE - Humidity: 79% - Pressure: 1006 hPa

Which is probably not all that helpful. But it will produce a CSV file of that data.

However, if we put the command to produce output, in a script and then run this script as a cron job, we should be able to generate a weather report on a daily basis for a specific location.

I have made a post (scheduled for later today) on cron and used this as an example of using cron, a script to produce a CSV file of date, time, weather info over time.

This is clearly more useful than what it produces above.

Tags


Mastodon ShellLabs Join Mastodon
AI statement : Consent is NOT granted to use the content of this blog for the purposes of AI training or similar activity. Consent CANNOT be assumed, it has to be granted.

Donate using Liberapay