Skip to content

Instantly share code, notes, and snippets.

@JuanjoSalvador
Created July 9, 2015 15:32
Show Gist options
  • Select an option

  • Save JuanjoSalvador/2751e66bf13cec7fdb36 to your computer and use it in GitHub Desktop.

Select an option

Save JuanjoSalvador/2751e66bf13cec7fdb36 to your computer and use it in GitHub Desktop.
Get current temp at your localization from CLI
#!/bin/bash
# To capitalize city name.
function toCapital
{
for x in $*
do
echo -n ${x:0:1} | tr '[a-z]' '[A-Z]' | xargs echo -n
echo -n ${x:1} | tr '[A-Z]' '[a-z]' | xargs echo -n
done
}
# Read a city name
echo -n "Input a city: "
read city
# Using the Yahoo Weather API and cURL, gets the temp from input city
curl -s https://query.yahooapis.com/v1/public/yql \
-d q="select item.condition from weather.forecast where woeid in (select woeid from geo.places(1) where text='$city') and u = 'c'" \
-d format=json > weather.json
# That autoformat JSON file to make it readable
cat weather.json | python -mjson.tool > weather_format.json
# Extract only the temp from JSON
temp=`cat weather_format.json | grep "temp" | cut -c 34-35`
# Capitalize the city name
capital_city=`toCapital $city`
# Output
echo "Temp at $capital_city: $temp ºC"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment