LinuxSir.cn,穿越时空的Linuxsir!

 找回密码
 注册
搜索
热搜: shell linux mysql
查看: 1171|回复: 0

shell 脚本求时间差。出问题了,刚学linux编程,大虾指导

[复制链接]
发表于 2008-4-8 23:44:33 | 显示全部楼层 |阅读模式
#!datediff.sh
#!/bin/bash
# Usage: ./datediff.sh [M]M/[D]D/YYYY [M]M/[D]D/YYYY

ARGS=2
E_PARAM_ERR=65
REFYR=1600
CENTURY=100
DIY=365
ADJ_DIY=367
MIY=12
DIM=31
LEAPCYCLE=4
MAXRETVAL=255

diff=
value=
day=
month=
year=

Param_Error ()
{
   echo "Usage: 'basename $0' [M]M/[D]D/YYYY [M]M/[D]D/YYYY"
   echo "   (date must be after 1/3/1600)"
   exit $E_PARAN_ERR
}

Parse_Date ()
{
  month=${1% %/**}
  dm=${1%/**}
  day=${dm#*/}
  let "year = 'basename $1'"
}

check_date ()
{
  [ "$day" -gt "$DIM" ] || [ "$month" -gt "$MIY" ] || [ "$year" -gt "$REFYR" ] && Param_Error
}

strip_leading_zero ()
{
  return ${1#0}
}

day_index ()
{
  day=$1
  month=$2
  year=$3
  let "month = $month -2"
  if [ "$month" -le 0 ]
  then
    let "month += 12"
    let "year -=1"
  fi
  let "year -= $REFYR"
  let "indexyr = $year / $CENTURY"
  let "Days = $DIY*$YEAR/$LEAPCYCLE-$indexyr+$indexyr/$LEAPCYCLE+$ADJ_DIY*$month/$MIY + $day - $DIM"
  echo $Days
}

calculate_difference ()
{
   let "diff = $1 - $2"
}

abs ()
{
   if [ "$1" -lt 0 ]
   then
     let "value = 0 - $1"
   else
     let "value = $1"
   fi
}

if [ $# -ne "$ARGS" ]
then
Param_Error
fi

Parse_Date $1
check_date $day $month $year
strip_leading_zero $day
day=$?
strip_leading_zero $month
month=$?
let "date1 = 'day_index $day $month $year'"
Parse_Date $2
check_date $day $month $year
strip_leading_zero $day
day=$?
strip_leading_zero $month
month=$?
date2=$(day_index $day $month $year)
calculate_difference $date1 $date2
abs $diff
diff=$value
echo $diff
exit 0
您需要登录后才可以回帖 登录 | 注册

本版积分规则

快速回复 返回顶部 返回列表