範囲が小さい (7 日間) ことを考えると、単純にすべての日付を生成する方がはるかに簡単です。
これを行うスクリプトは次のとおりです。
#!/bin/bash
fromDate="$1"
toDate="$2"
# break the dates into array so that we can easily access the day, month and year
IFS='.' read -a fromDateParts <<< "$fromDate"
IFS='.' read -a toDateParts <<< "$toDate"
# loop until the days, months and years match on both dates
while [[ "${toDateParts[0]}" -ne "${fromDateParts[0]}" || "${toDateParts[1]}" -ne "${fromDateParts[1]}" || "${toDateParts[2]}" -ne "${fromDateParts[2]}" ]]
do
# add the date to the pattern
printf -v date "%d%02d%02d" "${fromDateParts[2]}" "${fromDateParts[1]}" "${fromDateParts[0]}"
pattern="$pattern|$date"
((fromDateParts[0]++))
# reset days and increment month if days exceed 31
if [[ "${fromDateParts[0]}" -gt 31 ]]
then
fromDateParts[0]=1
((fromDateParts[1]++))
# reset month and increment year if months exceed 12
if [[ "${fromDateParts[1]}" -gt 12 ]]
then
fromDateParts[1]=1
((fromDateParts[2]++))
fi
fi
done
# finally add the toDate to the pattern
printf -v date "%d%02d%02d" "${toDateParts[2]}" "${toDateParts[1]}" "${toDateParts[0]}"
pattern="$pattern|$date"
# print the pattern, removing the first pipe
echo "${pattern#?}"
使用例:
$ dateRange.sh 28.1.2012 4.2.2012
20120128|20120129|20120130|20120131|20120201|20120202|20120203|20120204
$ dateRange.sh 31.12.2011 6.1.2012
20111231|20120101|20120102|20120103|20120104|20120105|20120106
$ dateRange.sh 28.1.2012 4.2.2012
20120128|20120129|20120130|20120131|20120201|20120202|20120203|20120204