0
votes

I have a script that i scheduled for weekdays with cron. The script uses wget to download a file every business day. The url is dynamic based on the date. I created the following script:

day_or_weekend=`date +%w`
if [$day_or_weekend == 1] ; then
    look_back=3
else
    look_back=1
fi

wget -O file_name_`date +%Y%m%d`.csv https://website/filename/date/`date $look_back days ago + %Y-%m-%d`/type/csv

This script generates the file with the correct name but the contents are empty. I'm quite new to writing bash shell scripts so im not sure how to go about debugging this. So my questions are:

  1. Am I defining the look_back variable correctly?
  2. Am I correctly adding the date parameter to the wget url?
1

1 Answers

1
votes

This should solve your blank issues. Command lines are very particular about spacing and quoting.

wget -O "file_name_$(date +%Y%m%d).csv" \
  "https://website/filename/date/$(date -d "$look_back days ago" +%Y-%m-%d)/type/csv"

Your logic to determine the previous work day will function as designed so long as you use busybox or GNU date (BSD date must subtract time in seconds from the epoch representation, basic Posix date can't actually do this).