Skip to content

Instantly share code, notes, and snippets.

@ravikant-pal
Last active July 15, 2025 12:07
Show Gist options
  • Save ravikant-pal/3f68640f5d62bdd2e7bf7dc3cfb0370b to your computer and use it in GitHub Desktop.
Save ravikant-pal/3f68640f5d62bdd2e7bf7dc3cfb0370b to your computer and use it in GitHub Desktop.
Weather Notifier — Real-Time Shell Scripting Exercise

🌤️ Weather Notifier — Real-Time Shell Scripting Exercise

⏱ Duration: 30 minutes

📍 Location: Pune

🧪 Tools: Bash, curl, awk, cron, Open Source API (wttr.in)


🎯 Exercise Goal

Write a Bash script that:

  • Fetches current weather in Pune from a public API
  • Extracts the weather condition and temperature
  • Logs personalized advice (e.g., “Carry umbrella ☔” or “Wear sunscreen 😎”)
  • Runs every hour automatically using cron

🛠 How to Run the Script

  1. Make the script executable:

    chmod +x weather_notify.sh
  2. Run it manually:

    ./weather_notify.sh
  3. Check the weather log:

    cat ~/weather.log
  4. Schedule it to run every hour using cron:

    crontab -e

    Add this line:

    0 * * * * /full/path/to/weather_notify.sh

🧪 Sample Log Output

[2025-07-12 13:00] Pune: Sunny +34°C — Wear sunglasses 😎 and sunscreen 🧴
[2025-07-12 14:00] Pune: Light rain +28°C — Carry an umbrella ☔

📚 Learning Outcomes

Feature Real-Life Use Case
curl Consuming APIs in scripts
awk, case Text parsing + conditional logic
cron Background scheduling for automation
Logging Creating traceable audit reports

💡 Optional Extensions

  • Monitor weather in multiple cities with a loop
  • Use notify-send for desktop notifications (Linux GUI)
  • Send messages via Telegram bot or Slack webhook
  • Add sound alerts for rain using aplay or mpg123

✅ Why This Matters

This script introduces you to API integration and job scheduling — two key building blocks in automation, DevOps, and real-time alerting systems.

“The same tools used to monitor a Kubernetes cluster can help you decide when to carry a raincoat.” ☔

#!/bin/bash
# weather_notify.sh — logs weather advice based on real Pune weather
CITY="Pune"
DATA=$(curl -s "wttr.in/$CITY?format=%C+%t")
# Extract weather condition and temperature
CONDITION=$(echo "$DATA" | awk '{$NF=""; print $0}' | sed 's/ *$//')
TEMP=$(echo "$DATA" | awk '{print $NF}')
# Create advice based on condition
if [[ "$CONDITION" == *"Rain"* || "$CONDITION" == *"Shower"* || "$CONDITION" == *"Thunder"* ]]; then
ADVICE="Carry an umbrella ☔"
elif [[ "$CONDITION" == *"Sunny"* || "$CONDITION" == *"Clear"* ]]; then
ADVICE="Wear sunglasses 😎 and sunscreen 🧴"
elif [[ "$CONDITION" == *"Cloudy"* || "$CONDITION" == *"Overcast"* ]]; then
ADVICE="A bit gloomy – light jacket maybe? 🧥"
else
ADVICE="Be weather-wise! 📡"
fi
# Log to file
echo "[$(date '+%Y-%m-%d %H:%M')] $CITY: $CONDITION $TEMP$ADVICE" >> ~/weather.log
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment