Search code examples
macosshellvariableswebaddress

terminal / shell script: adding variable to web address - OS X


I am trying to automate downloading our weather data for our school. I'm not a huge tech guy but I'm the best at the school I suppose. My problem is trying to insert the time variables into the web address. Here is what I have done so far.

Currently This works:

curl -o /Library/Server/Web/Data/Sites/wupmooksgmol.ca/weather/"$(date +%Y)"/"$(date +%Y-%m-%d)".weather.csv 'http://www.wunderground.com/weatherstation/WXDailyHistory.asp?ID=IBRITISH322&day=16&month=1&year=2015&graphspan=day&format=1'

But, in the web address, it is only downloading the Jan. 16th 2015 weather data. I want to put in the current day, month and year into the web address itself. Thus, at 23:57 each day it downloads the weather data for that day. I have tried many variation on the following but no luck:

curl -o /Library/Server/Web/Data/Sites/wupmooksgmol.ca/weather/"$(date +%Y)"/"$(date +%Y-%m-%d)".weather.csv 'http://www.wunderground.com/weatherstation/WXDailyHistory.asp?ID=IBRITISH322&day=“$(date +%d)”&month=“$(date +%m)”&year=“$(date +%Y)”&graphspan=day&format=1'

I have also attempted a numerous variations on this shell script:

#!/bin/bash

day=$(date '+%d')
month=$(date '+%m')
year=$(date '+%Y')
ymd=$(date '+%Y-%m-%d')

curl -o /Library/Server/Web/Data/Sites/wupmooksgmol.ca/weather/“$year"/"$ymd".weather.csv 'http://www.wunderground.com/weatherstation/WXDailyHistory.asp?ID=IBRITISH322&day=“$day”&month=“$month”&year=“$year”&graphspan=day&format=1'

Thanks for any help you can provide.


Solution

  • I think you need this:

    #!/bin/bash
    
    day=$(date '+%d')
    month=$(date '+%m')
    year=$(date '+%Y')
    ymd=$(date '+%Y-%m-%d')
    
    curl -o weather.csv "http://www.wunderground.com/weatherstation/WXDailyHistory.asp?ID=IBRITISH322&day=${day}&month=${month}&year=${year}&graphspan=day&format=1"
    

    The way I think about quotes is like this. If you surround stuff with single quotes, nothing is going to get expanded and there is no point putting variables inside them. If you use double quotes, variables will get expanded and stuff with spaces in will get "held together" and treated as a single parameter. Not very technical, but it works.