About a third of the way into Hacker School, I got myself entrenched in a rather unusual project—trying to figure out how to get an automatically updated weather icon into my Bash prompt, doing it purely in Bash for practical (and aesthetic) reasons.
I called it BashWeather.
There are a number of configurable options, including the following:
- How often to wait between weather updates
- The default character to be supplied in the event that the weather isn’t available
- The method to use to determine where in the world the computer is located. Options are to use IP address, to use the built-in Mac locator if available, or to supply the city and country in a string when calling the script.
- Allowable timeout for the HTTP requests BashWeather makes
I learned quite a bit from this project about Bash functions, scoping, some of Bash’s oddities (behavior of brackets, globbing, needing to quote variables, etc.), and some of the nitty-gritty details of Bash variables. Since a lot of global variables were required by the script to keep track of state, like how long it’s been since the last check and whether or not internet is available, I was very careful to pollute the global namespace as little as possible, which was an interesting exercise given that Bash functions can’t actually return values to the caller in the way you’d traditionally think about it.
I couldn’t think of anything to do at Hacker School for a bit, and I was about to head outside to get a cup of coffee to clear my head, and I noticed that it was raining and got very upset that I didn’t have an umbrella with me. I almost always forget to bring my umbrella, and I used to use an IFTTT recipe that sent me a Gchat whenever it started to rain. Unfortunately, with the advent of Hangouts, it was eventually deprecated.
I liked the Gchat recipe because it alerted me both on my computer and on my phone. Considering how to replace it, I started thinking about what I look at more than anything else—and realized the one and only answer there was my Bash prompt. I’m always looking at the command line, one way or another, or at least have it open—and the decision was made.
From then on, I would know the weather.
But would you really ever look at the weather in your prompt?
When I started telling people about it at Hacker School I did encounter a bit of skepticism, usually along the lines of:
Why would you ever need the weather in your prompt?
Come on, you’re never actually going to use that.
I got this reaction from one person in particular, and we then sat down to start pairing on a quick question that we were trying to resolve—I think possibly something to do with BashWeather itself.
We’d been working not even two minutes when we turned to the prompt and he saw the umbrella (☂) in the prompt and exclaimed, “Oh no, is it raining? I didn’t realize!”
…and then he realized what had happened, and then I grinned.
And now, the code
The full script, with instructions, can be found at the Github repository. It is currently fully functional, though still a work in progress. I’ll be doing a longer post shortly on how I structured BashWeather from a scoping perspective, since I found the architecture required of a large Bash script to be very different and very interesting.