In the world of DevOps, we’re always looking for ways to automate and streamline our tasks. Even something as simple as checking the weather can be made more efficient with a bit of scripting magic. So, I recently decided to build a command-line interface (CLI) tool using Bash that fetches the current weather for my location.
Why a CLI?
You might be wondering, “Why bother with a CLI when there are countless weather apps and websites?” Well, here are a few reasons why I prefer a CLI approach:
- Efficiency: I can quickly get the weather information without leaving my terminal.
- Automation: I can easily integrate this script into other scripts or workflows.
- Learning: It’s a fun way to practice my Bash scripting skills.
The Journey Begins
My initial attempt relied on the Dark Sky API, which unfortunately was discontinued. This presented a great opportunity to explore other weather APIs and improve my script.
Choosing the Right Tools
After some research, I selected the following:
- ip-api.com: A free and reliable service for getting location information based on an IP address.
- Open-Meteo: An open-source weather API that provides a wide range of weather data, including current conditions and forecasts.
Here is the code:
#!/usr/bin/env bash
# Get the user's external IP address using ipecho.net
EXTIP="$(curl -s https://ipecho.net/plain)"
# Get location information (latitude and longitude) based on the IP address
LOCATION="$(curl -s http://ip-api.com/json/)"
LAT=$(echo "$LOCATION" | jq -r '.lat')
LON=$(echo "$LOCATION" | jq -r '.lon')
# Fetch weather data from Open-Meteo API using the latitude and longitude
WEATHER=$(curl -s "https://api.open-meteo.com/v1/forecast?latitude=$LAT&longitude=$LON¤t_weather=true&daily=temperature_2m_max,temperature_2m_min,weathercode")
# Extract the current temperature in Celsius from the API response
TEMP=$(echo "$WEATHER" | jq -r '.current_weather.temperature')
# Convert the temperature to Fahrenheit
fahrenheit=$(echo "scale=2; ($TEMP * 9 / 5) + 32" | bc)
# Extract the city and country from the location data
city=$(echo "$LOCATION" | jq -r '.city')
country=$(echo "$LOCATION" | jq -r '.country')
# Display the weather information to the user
echo "Current weather in $city, $country:"
echo "Temperature: $TEMP°C ($fahrenheit°F)"
This script does the following:
- Retrieves the user’s external IP address.
- Uses the IP address to get the location (latitude and longitude).
- Fetches the current weather data from Open-Meteo.
- Extracts the temperature in Celsius and converts it to Fahrenheit.
- Displays the weather information in a user-friendly format.
Lessons Learned
Building this CLI tool was a valuable learning experience. I gained a better understanding of:
- Bash scripting: Working with APIs, parsing JSON data, and using command-line tools like
curlandjq. - API selection: Evaluating different weather APIs and choosing the one that best suits my needs.
- Code readability: Writing clean and well-commented code.
Next Steps
This is just the beginning! I plan to further improve this script by:
- Adding error handling: To make the script more robust.
- Improving the output: Displaying more weather details and formatting the output for better readability.
- Exploring other APIs: Trying out different weather APIs to see what other data is available.
- Adding Modularity: Converting various parts of the script to functions to making things easier to read and manage.
Stay tuned for future updates and enhancements to this project. You can find the complete code on my GitHub repository: by clicking here, or by visiting the Projects page of this site.
I hope this inspires you to build your own CLI tools and automate your daily tasks. Happy Coding!!