2015-02-10 78 views
1

我有打印日期不正確逗號分隔日期的價值

condition=`date -d "20150209" +%a` 
start_date=`date -d -7day"20150209" +%Y%m%d` 
end_date=`date -d -1day"20150209" +%Y%m%d` 
if [ "$condition" = "Mon" ] 
then 
while [ "$start_date" != "$end_date" ] 
do 
Date+=$start_date, 
start_date=$(date -d +1day"$start_date" +%Y%m%d) 

done 
echo "$Date" 
fi 

我期待輸出

20150203,20150204,20150205,20150206,20150207,20150208 

我無法弄清楚什麼是錯誤的代碼。

+0

什麼是*實際*輸出? – 2015-02-10 14:20:29

+0

20150202,20150203,20150204,20150205,20150206,20150207,20150202,20150203,20150204,20150205,20150206,20150207, – 2015-02-10 14:31:56

+0

我得到:'20150202,20150203,20150204,20150205,20150206,20150204,'從上面的代碼。問題是什麼? – anubhava 2015-02-10 14:34:29

回答

0

我錯過了日期範圍中的最後一個元素。 這裏是解決方案。

condition=`date -d "20150209" +%a` 
start_date=`date -d -7day"20150209" +%Y%m%d` 
end_date=`date -d -1day"20150209" +%Y%m%d` 

while [ "$start_date" != "$end_date" ] 
do 
Date+=$start_date, 
start_date=$(date -d +1day"$start_date" +%Y%m%d) 
done 

echo "$Date$end_date" 

如果有人有更好的解決方案,請讓我知道。