3

This question already has an answer here:

For a project I want to use wget in a cron to download a data file. In the wget statement, a start- and enddate have to be defined in the following format:

wget --post-data="stns=235&vars=TEMP&start=YYYYMMDDHH&end=YYYYMMDDHH"

Since I want it to be done by a cron job, I would like the start- and enddate to be set automatically. More specific, I would like the startdate to be set to '1 hour ago' and the enddate to 'now'.

There has been a similar question in the post Using date -1day with wget. Here the suggested solution was to put the variables between single quotes, but this did not work. E.g.:

"[...]start='`date -d yesterday +%Y%m%d%H'&end=`date +%Y%m%d%H`"

I simply got the error "Error 400: Bad request" when trying to execute the wget-statement in the terminal.

Thank you.

marked as duplicate by Stéphane Chazelas shell-script Aug 10 '17 at 10:19

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

2

Within a cron job, % is special and must be escaped. Also, backquote syntax is best avoided. I would suggest something like the following:

wget --post-data="start=$(date ... +\%Y\%m\%d\%H)&end=$(date ... +\%Y\%m\%d\%H)&..."

Not the answer you're looking for? Browse other questions tagged or ask your own question.