Fetch the local weather and optionally put it on the footer of your motd

I wanted to put my local weather on the bottom of my /etc/motd - because i'm dorky like that.

I found a bunch of random ways to fetch the weather forecast in plain text... and a lot of complicated grep/sed/curl commands to get there. I wanted something as simple as possible.

So first, the shell script to get the weather in plain text...

echo " Weather for Palmyra, Virginia:"
lynx --dump "http://mobile.weather.gov/port_mp_ns.php?CityName=Palmyra&site=AKQ&State=VA&warnzone=VAZ048/" | head -n 7 | tail -n 1 | sed -e 's/\ //g' | sed -e 's/°/°/'

The echo is just for looks, obviously. You'll need to change it to whatever you want it to be headed up by... or remove it completely.

The first lynx command fetches the website how a browser sees it.
The second head takes only the top portion of that resulting website.
The third command, tail, only takes the bottom line of the resulting head display.
The fourth sed command takes all the spaces out.
The fifth command gets rid of some weird character interpritation from lynx and makes it nicely have a Fahrenheit symbol.

You'll need to get your own weather url and replace mine with it by entering your zip code here: http://mobile.weather.gov/.

Okay, so now we have a way to get the local weather. I actually setup an alias in .bashrc for this, too.

Next, we have the script that puts this at the bottom of your /etc/motd file. That script, in my case, assumes that you have sudo without entering your password:


cat ~/sh/backup/motd.backup > ~/motd.new
sh ~/sh/weather.sh >> ~/motd.new
echo "" >> ~/motd.new
sudo mv ~/motd.new /etc/motd

The first line copies my standard motd message into place within what will be the new motd file. This makes sure we maintain the same static message at the top. You'll have to update that line and point it to a basic motd message file that serves as a template.
The second line, runs my weather script, which you'll need to update and point to where yours is.
The third adds a blank line to the bottom of the new motd file, for better formatting.
The last of the 3 lines moves the new motd into place on top of your current.

Then, I scheduled that last script to run each hour in my user's crontab with the command 'crontab -e':
0 * * * * ~/sh/generateMOTD.sh