I have such bash script:
array=( '2015-01-01', '2015-01-02' )
for i in "${array[@]}"
do
python /home/user/executeJobs.py {i} &> /home/user/${i}.log
done
Now I want to loop through a range of dates, e.g. 2015-01-01 until 2015-01-31.
How to achieve in Bash?
Update:
Nice-to-have: No job should be started before a previous run has completed. In this case, when executeJobs.py is completed bash prompt $
will return.
e.g. could I incorporate wait%1
in my loop?
Best Answer
Using GNU date:
Note that because this uses string comparison, it requires full ISO 8601 notation of the edge dates (do not remove leading zeros). To check for valid input data and coerce it to a valid form if possible, you can use
date
as well:One final addition: To check that
$startdate
is before$enddate
, if you only expect dates between the years 1000 and 9999, you can simply use string comparison like this:To be on the very safe side beyond the year 10000, when lexicographical comparison breaks down, use
The expression
$(date -d "$d" +%Y%m%d)
converts$d
to a numerical form, i.e.,2015-02-23
becomes20150223
, and the idea is that dates in this form can be compared numerically.