From a61f9f2d2d2101b9cdb564cbe74b7e239086f0a9 Mon Sep 17 00:00:00 2001 From: Ratatoskr Date: Thu, 8 Feb 2024 16:03:47 +0100 Subject: [PATCH] Initial commit of weather.sh script This script retrieves current weather information from OpenWeatherMap API. It includes: - OpenWeatherMap API key for data retrieval. - Location settings for specifying the city and country. - API request using cURL to fetch weather data. - Extraction of relevant information from the JSON response using jq. - Conversion of temperature from Kelvin to Celsius. - Formulation of the weather information in a formatted English text. - Output of the weather information. Author: Michael Haider Date: 2024-02-08 --- weather.sh | 40 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 40 insertions(+) create mode 100644 weather.sh 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" +