日期:2014-05-16  浏览次数:20924 次

小弟今天学到了if语句和case语句,写了个时间脚本,求精简!
用了一下午时间,写了个判断时间的脚本,脚本后面加时间 输出结果为下一天。
#!/bin/sh
fileyear=`echo $1|cut -c 1-4`
filemonth=`echo $1|cut -c 5-6`
fileday=`echo $1|cut -c 7-8`
case $filemonth in
  01|03|05|07|08|10)
if [ ${fileday} == "31" ];then
  days=01
  months=`expr ${filemonth} + 1`
  years=$fileyear
else
  days=`expr ${fileday} + 1`
  months=$filemonth
  years=$fileyear
fi
nextday=${years}${months}$days
;;
04|06|09|11)
if [ ${fileday} == "30" ];then
  days=01
  months=`expr ${filemonth} + 1`
  years=$fileyear
else
  days=`expr ${fileday} + 1`
  months=$filemonth
  years=$fileyear
fi
nextday=${years}${months}$days
;;
02)
if [ `expr $fileyear % 4` -eq 0 ]; then
 if [ `expr $fileyear % 400` -eq 0 ];
 then days=29
 elif [ `expr $fileyear % 100` -eq 0 ];
 then days=28
else days=29
 fi
else
days=28
fi
if [ $days == $fileday ];then
  days=01
  months=`expr ${filemonth} + 1`
  years=$fileyear
else
  days=`expr ${fileday} + 1`
  months=$filemonth
  years=$fileyear
fi
nextday=${years}${months}$days
;;
12) days=31
if [ $days == $fileday ];then
days=01
months=01
years=`expr $fileyear + 1`
else
  days=`expr ${fileday} + 1`
  months=$filemonth
  years=$fileyear
fi
nextday=${years}${months}$days
;;# ./nextday.sh 20110228
2011301
# ./nextday.sh 20000228
20000229
# ./nextday.sh 20111231
20120101
# ./nextday.sh 20110228
2011301
# ./nextday.sh 20120229
2012301
# ./nextday.sh 20000229
2000301
# ./nextday.sh 21000228
2100301
esac
echo $nextday

特殊日子结果为:



------解决方案--------------------
探讨
受教了~~~ 这个脚本最后是放在SUN的系统里面用,[[ ... ]]符号不支持啊