diff --git a/weather.sh b/weather.sh new file mode 100644 index 0000000..931f48c --- /dev/null +++ b/weather.sh @@ -0,0 +1,40 @@ +#!/bin/bash + +################################################################################ +# OpenWeatherMap Weather Script +# Author: Michael Haider +# Date: 2024-02-08 +# +# This script retrieves current weather information from OpenWeatherMap API +# based on the specified city and country. It then extracts relevant data, +# converts temperature from Kelvin to Celsius, and outputs the information +# in a formatted English text. +################################################################################ + +# OpenWeatherMap API key +api_key="da7765a8c6d2975b7dfe68f2a1775631" + +# Location settings +city="Pfarrkirchen" +country="de" + +# Make API request and retrieve data +weather_data=$(curl -s "http://api.openweathermap.org/data/2.5/weather?q=${city},${country}&appid=${api_key}") + +# Extract relevant information from the JSON response +description=$(echo "$weather_data" | jq -r '.weather[0].description') +temperature=$(echo "$weather_data" | jq -r '.main.temp') +humidity=$(echo "$weather_data" | jq -r '.main.humidity') +wind_speed=$(echo "$weather_data" | jq -r '.wind.speed') + +# Convert temperature from Kelvin to Celsius +temperature_celsius=$(awk "BEGIN {print $temperature - 273.15}") + +# Formulate the text in English +weather="Current weather in $city: $description. +The temperature is ${temperature_celsius}°C, humidity is ${humidity}%. +Wind speed is ${wind_speed} m/s." + +# Output the text +echo "$weather" +