It looks cold outside. Wouldn’t it be nice to know exactly what to put on before rushing through the door? I could as well dig my phone up from my pocket and check the outer temperature from AccuWeather, or even walk those 4 steps to the thermometer and see by myself, but why bother? I have the command line open already!
Great! No jacket, that is. How to do this? I used wget to fetch the temperature data from a weather site (weather.savonia.fi in my case), followed by a perl one-liner for parsing the temperature value from the wget output. The source of the weather site has a line like this:
Yeah, it’s partly in Finnish, but it’s just a simple HTML table row containing two cells. The first cell, “Lämpötila” (“Temperature” in English), is the name of the row, and the second cell contains the temperature value (11,6 degrees Celcius, which is pretty warm here in Finland). I’m sure most weather sites have nice APIs for stuff like this, though.
The perl part of the script looks for the characters between ‘<td class=”weather_arvo”>’ and the degree sign (°), and passes the chars into its standard output. Just so that things don’t accidentally get too simple, in Finland we use comma as a decimal delimiter instead of a period, so the last part of the script is a sed command for converting the comma into a period (I had to do this so I could eventually make a graph of the values with gnuplot). And the working script looks like this (modify to suit your location and weather data provider as needed):
wget http://weather.savonia.fi/data.php?3 -q -O - | perl -ne 'printf qq[%s\n], $1 if m/<td class="weather_arvo">([^<]+)°/' | sed s/\\,/./g
Assuming that the script is saved as “temp.sh” in the user’s home directory, adding an alias declaration to the user’s .bashrc file would be done by adding in the following line:
After reloading .bashrc the temperature script can be executed with just “temp” from anywhere in the system. Append the output into a file with “temp >> temps.txt”, or insert the temperatures into a database by piping the command to mysql, executing every hour as a cron job!
How cool is that, or should I say, how warm is that?