Check Temperature – A Geeky BASH Script

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 ( 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:

<tr class=”weather_row“><td class=”weather_kuvaus“>Lämpötila</td>
<td class=”weather_arvo“>11,6°C</td></tr>

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 -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 “” 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:

alias temp=’/home/user/’

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?



  1. Nice! Like you said most of the public weather services have an API for this and that’s what normally should be used. Parsing HTML works nicely but since both data and presentation is tied together it’s problematic if the presentation changes, then you need to change your parser 🙂 Here it’s most likely not a relevant issue but could be worth mentioning as a general principle. Good stuff!


    1. You’re absolutely right. Though, if I get the weather information from a nice API, where would the dirty hacking part go? 🙂 Thanks for your comment!

      Liked by 1 person

      1. Oh, true. That’s the beauty of doing things like a hacker 🙂


Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s

%d bloggers like this: