YYYYMMDD格式的Korn Shell增量日期
我需要使用特定格式YYYYMMDD(例如:20190401或20190523)增加日期。0需要在那里停留个位数的天数和月份 我希望输入cmd行args ex./script.ksh(#天增量)(开始日期)或./script.ksh(开始日期)(结束日期) 我制作了一个糟糕的版本,它接受了./script.ksh(#of days)(year)(month)(day)ex:./script.ksh 7 2019 04 10YYYYMMDD格式的Korn Shell增量日期,shell,unix,ksh,Shell,Unix,Ksh,我需要使用特定格式YYYYMMDD(例如:20190401或20190523)增加日期。0需要在那里停留个位数的天数和月份 我希望输入cmd行args ex./script.ksh(#天增量)(开始日期)或./script.ksh(开始日期)(结束日期) 我制作了一个糟糕的版本,它接受了./script.ksh(#of days)(year)(month)(day)ex:./script.ksh 7 2019 04 10 #The amount of days to load by Days=$
#The amount of days to load by
Days=$1
Year=$2
Month=$3
Day=$4
count=0
while (($count < $Days))
do
#The date of the file in format: YYYYMMDD
if [[ $Day -eq "1" || $Day -eq "2" || $Day -eq "3" || $Day -eq "4" || $Day -eq "5" || $Day -eq "6" || $Day -eq "7" || $Day -eq "8" || $Day -eq "9" ]];
then
Day=0$Day
fi
Date=$Year$Month$Day
print $Date
#Check if month then day and increment accordingly
#months with 31 days
if [[ ($Month -eq "01" || $Month -eq "03" || $Month -eq "05" || $Month -eq "07" || $Month -eq "08" || $Month -eq "10" || $Month -eq "12") && ($Day == 31)]];
then
#If Dec, 31 XXXX
if [[ $Month -eq "12" && Day == 31 ]];
then
Month=01
Day=01
Year=$Year+1
fi
if((Day == 31));
then
Month=$Month+1
Day=1
fi
#Months with 30 days
elif [[ ($Month -eq "04" || $Month -eq "06" || $Month -eq "09" || $Month -eq "11") && ($Day == 30) ]];
then
if(($Day == 30));
then
$Month=$Month+1
Day=01
fi
#The tricky February leap year
elif [[ ($Month -eq "02") && ($Day == 28 || $Day == 29) ]];
then
leapcheck=$(($Year % 4))
if(( leapcheck == 0 && Day == 29 ));
then
Month=$Month+1
Day=01
elif(( $leapcheck != 0 && $Day == 28 ));
then
Month=$Month+1
Day=01
fi
else
(( Day=$Day+1 ))
fi
((count=$count+1))
done
#要加载的天数
天数=1美元
年份=$2
月份=$3
日=4美元
计数=0
而(($count<$Days))
做
#文件的日期,格式为:YYYYMMDD
如果[$Day-eq“1”|$$Day-eq“2”|$$Day-eq“3”|$$Day-eq“4”|$$Day-eq“5”|$$Day-eq“6”|$$Day-eq“7”|$Day-eq“8”|$Day-eq“9”];
然后
天=0美元天
fi
日期=$年$月$日
打印$Date
#检查是否月然后日,并相应增加
#月加31天
如果[($Month-eq“01”| |$Month-eq“03”| |$Month-eq“05”|$Month-eq“07”|$Month-eq“08”|$Month-eq“10”| |$Month-eq“12”)&($Day==31)];
然后
#如果是12月31日XXXX
如果[$Month-eq“12”和&Day==31];
然后
月份=01
日=01
年份=$Year+1
fi
如果((天=31));
然后
月份=$Month+1
天=1
fi
#月加30天
elif[($Month-eq“04”| |$Month-eq“06”| |$Month-eq“09”| |$Month-eq“11”)&($Day==30)];
然后
若($Day==30));
然后
$Month=$Month+1
日=01
fi
#棘手的二月闰年
elif[[($Month-eq“02”)&&($Day==28 | |$Day==29)];
然后
leapcheck=$($年%4))
如果((leapcheck==0&&Day==29));
然后
月份=$Month+1
日=01
elif(($leapcheck!=0&&$Day==28));
然后
月份=$Month+1
日=01
fi
其他的
((天=$Day+1))
fi
((计数=$count+1))
完成
将打印20190410至20190416。但是,在删除0的月份和天数递增时会出现问题,而对于我的月份,则根本不会递增。我相信一定有比我的尝试更简单更好的方法。我不熟悉korn shell脚本。如果您有(使用-d
选项),一种简单的方法是将日期转换为自历元起的秒数,添加以秒为单位的天数,然后将其转换回您想要的格式:
days=5
now=20190401
now_sec=$(date -d "${now:0:4}-${now:4:2}-${now:6:2} 11:00:00" +%s)
then_sec=$((now_sec + days * 3600 * 24))
then=$(date -d @"$then_sec" +%Y%m%d)
echo "$then" # will output 20190406
${now:0:4}
提取字符串的一部分,请参见。如果您有(使用-d
选项),一种简单的方法是将日期转换为从纪元开始的秒数,添加秒数,然后将其转换回所需格式:
days=5
now=20190401
now_sec=$(date -d "${now:0:4}-${now:4:2}-${now:6:2} 11:00:00" +%s)
then_sec=$((now_sec + days * 3600 * 24))
then=$(date -d @"$then_sec" +%Y%m%d)
echo "$then" # will output 20190406
${now:0:4}
提取字符串的一部分,请参阅。您是否有一个版本的日期
命令(例如GNU版本)可以让您相对轻松地完成任务?你考虑过这个选择吗?如果做不到这一点,您能否使用Perl或其他脚本语言,使用内置或扩展模块简化计算?您是否有一个版本的date
命令(例如GNU版本)可以让您相对轻松地完成这项工作?你考虑过这个选择吗?如果做不到这一点,您是否可以使用Perl或其他脚本语言,并使用内置或扩展模块来简化计算?