【发布时间】:2015-03-06 03:34:27
【问题描述】:
我有一个名为 myScript 的 unix 脚本,其中两个参数应以 YYYYMM 格式传递,指示脚本的开始月份和结束月份。
在脚本内部,根据参数,我需要在第一个参数中计算月份的开始日期,在第二个参数中计算月份的结束日期。
我写了以下代码:
startYr=`expr substr $2 1 4`
startMonth=`expr substr $2 5 2`
startDate=`cal $startMonth $startYr | grep . | fmt -1 | tail -1`
endYr=`expr substr $3 1 4`
endMonth=`expr substr $3 5 2`
endDate=`cal $endMonth $endYr | grep . | fmt -1 | tail -1`
export eomStartDate=$startYr-$startMonth-$startDate
export eomEndDate=$endYr-$endMonth-$endDate
echo "START DATE: $eomStartDate"
echo "END DATE: $eomEndDate"
但是,在运行上述脚本时:
myScript 201401 201412
我得到的结果是:
START DATE: 2014-12-31
END DATE: --31
我错过了什么?我正在使用 Korn Shell。
感谢阅读!
【问题讨论】: