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
41 lines
1.4 KiB
Bash
41 lines
1.4 KiB
Bash
#!/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"
|
|
|