0

Can someone help me with converting WEATHER_TEMP so it outputs a value in Fahrenheit. I believe the formula for Fahrenheit is ((Celsius * 1.8) + 32) however every time I try and write the value for the output it doesn't work. See code below. Thank you!

Note: I've replaced the API Info for privacy reasons.

#!/usr/bin/env bash

command -v jq >/dev/null 2>&1 || { echo >&2 "Program 'jq' required but it is not installed.  
Aborting."; exit 1; }
command -v wget >/dev/null 2>&1 || { echo >&2 "Program 'wget' required but is not installed.  
Aborting."; exit 1; }

APIKEY="XXXXX"
#ZIPCODE="XXXX"
CITY_ID="XXXX"
URL="http://api.openweathermap.org/data/2.5/weather?id=${CITY_ID}&units=metric&APPID=${APIKEY}"

WEATHER_RESPONSE=$(wget -qO- "${URL}")

WEATHER_CONDITION=$(echo $WEATHER_RESPONSE | jq '.weather[0].main' | sed 's/"//g')
WEATHER_TEMP=$(echo $WEATHER_RESPONSE | jq '.main.temp')
WIND_DIR=$( echo "$WEATHER_RESPONSE" | jq '.wind.deg')
WIND_SPEED=$( echo "$WEATHER_RESPONSE" | jq '.wind.speed')
F_WEATHER_TEMP=$(echo ($WEATHER_TEMP * 1.8) + 32)

WIND_SPEED=$(awk "BEGIN {print 60*60*$WIND_SPEED/1000}")
WIND_DIR=$(awk "BEGIN {print int(($WIND_DIR % 360)/22.5)}")
DIR_ARRAY=( N NNE NE ENE E ESE SE SSE S SSW SW WSW W WNW NW NNW N )
WIND_DIR=${DIR_ARRAY[WIND_DIR]}

case $WEATHER_CONDITION in
  'Clouds')
    WEATHER_ICON=""
    ;;
  'Rain')
    WEATHER_ICON=""
    ;;
  'Snow')
    WEATHER_ICON=""
    ;;
  *)
    WEATHER_ICON=""
    ;;
esac

echo "${WEATHER_ICON}  ${WEATHER_TEMP}°C:"
Abilogos
  • 4,777
  • 2
  • 19
  • 39
  • Hi , welcome to stackoverflow community by doesnt work, what do you mean? it doesnt print anything or wrong result? – Abilogos Aug 22 '21 at 14:26
  • 1
    consider updating the question to provide a [minimal, reproducible example](https://stackoverflow.com/help/minimal-reproducible-example) of your issue; for example, all of the `wind` and `icon` stuff is a distraction ... provide just the pieces of code related to the `C->F` calculation; consider providing the complete result of the `wget` call and then go from there re: variables/calculations – markp-fuso Aug 22 '21 at 14:34

1 Answers1

0

I believe you can use imperial in the unit:

URL="http://api.openweathermap.org/data/2.5/weather?id=${CITY_ID}&units=imperial&APPID=${APIKEY}"

and it will response you with Fahrenheit. no need for conversion. ref

using expr

or you can use the expr to evaluate arithmetic operation in bash. like:

F_WEATHER_TEMP=$(expr $(expr $WEATHER_TEMP * 1.8 ) + 32 )
Abilogos
  • 4,777
  • 2
  • 19
  • 39