免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 3068 | 回复: 9
打印 上一主题 下一主题

日期偏移shell [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2003-06-24 12:34 |只看该作者 |倒序浏览
为了解决一个实际问题,这是我第一次编写shell脚本,其中引用red sleeve的一部分代码,希望大家指正
#!/bin/sh

#print information(打印消息)
prn() {
   echo $*
}
#print information(打印消息)

#check parameter is number(检查输入是否是数字)
#parameters (参数)
#number that will be checked(要检查的内容)
#[err info](错误信息,可选)
isnum(){
  if [ $# -ge 1 ]; then
    ok=`echo $1|sed -n '/^[0-9][0-9]*$/p'`
    if [ -n $ok ]; then
       return 0
    else
       if [ $# -eq 2 ]; then
         prn $2
       fi  
       return 1
    fi
  else
    return 1
  fi      
}
#check parameter is number(检查输入是否是数字)

#get the month's days(得到指定年月的天数)
#parameters(参数)  
#year(年)
#month(月)
leap() {
    if [ $# -eq 2 ]; then
            case $2 in
            1|3|5|7|8|10|12)
                Max=31
                ;;
            4|6|9|11)
                Max=30
                ;;                      
            2)
               #div by 4 and not by 100 or div by 400 is leap month(闰年-被4整除不能被100整除;被400整除)
               if [ `rmd $1 4` -eq 0 -a `rmd $1 100` -ne 0 -o  `rmd $1 400` -eq 0 ]; then
                   Max=29
               else
                   Max=28
               fi
               ;;
            *) Max=0
            esac
            echo $Max
    else
       echo 0
    fi     
}
#get the month's days(得到指定年月的天数)

#add operation(加)
#parameters(参数)
#op1(参数1)
#op2(参数2)
add() {
  echo `expr $1 + $2`
}

#subtration operation(减)
#parameters(参数)
#op1(参数1)
#op2(参数2)
sub() {
  echo `expr $1 - $2`
}

#multiply operation(乘)
#parameters(参数)
#op1(参数1)
#op2(参数2)
mult() {
  echo `expr $1 \* $2`
}

#div operation(除)
#parameters(参数)
#op1(参数1)
#op2(参数2)
div() {
  echo `expr $1 / $2`
}

#remainder operation(余)
#parameters(参数)
#op1(参数1)
#op2(参数2)
rmd() {
  echo `expr $1 % $2`
}

#get lens of string(串长)
#parameters(参数)
#str(串)
strlen() {
  echo `expr $1 : '.*'`
}

#cal time offset(计算偏移时间)
#parameters(参数)
#offset(偏移量,单位为分钟,正数向后偏移,负数向前偏移)
#time(基准时间,如不给出取系统时间)
#note:the offset can not be greate than one month(注:目前只能处理1个月内的偏移)
dayoff() {
  info="syntax: dayoff '30 200306230000',note 30'units is minute or dayoff 30"
  if [ $# -ge 1 ]; then
     #check offset format,offset must be number and it can begin with '+' or '-'
     op=`echo $1|cut -b 1-1`
     len=`expr $1 : '.*'`
     if [ $op != "+" -a $op != "-" ]; then
        op="+"
        off=$1
     else
        off=`echo $1|cut -b 2-$len`
     fi
     isnum $off $info
     if [ $? -ne 0 ]; then
       return 1
     fi
     
     #check time format,if time param not supply get system time
     if [ $# -eq 2 ]; then
            time=$2
            #get string length and check parameters
            len=`strlen "$time"`
            #check time lenth
            if [ $len -gt 12 ]; then
               prn $info
               return 1
            elif [ $len -lt 12 ]; then
                prn $info
                return 1
            fi
            #check time is num
            isnum $2 $info
            if [ $? -ne 0 ]; then
              return 1
            fi
            
            #get each part of time
            year=`echo $time | cut -b 1-4`
            month=`echo $time | cut -b 5-6`
            month=`sub $month 0`
        daymax=`leap $year $month`
            day=`echo $time | cut -b 7-8`
            hour=`echo $time | cut -b 9-10`
            minute=`echo $time | cut -b 11-12`
            if [ $month -gt 12 -o $month -lt 1 -o $day -gt $daymax -o $day -lt 1 -o $hour -gt 23  \
            -o $hour -lt 0 -o $minute -gt 59 -o $minute -lt 0 ]; then
                 prn "Invalid time range. \n" $info
                 return 1
            fi
     else
        year=`date +%Y`
        month=`date +%m`
        month=`sub $month 0`
        daymax=`leap $year $month`
        day=`date +%d`
        hour=`date +%H`
        minute=`date +%M`
     fi
     #get days of the month
     #deal each condition
     #"+"condition
     if [ $op = "+" ]; then
        #get total of minutes
        total=`add  $off  $minute`
        #cal muniute part
        minute=`rmd $total 60`
        #get total of hours
        total=`div $total  60`
        total=`add  $total $hour`
        #cal hour part
        hour=`rmd $total 24`
        #get total of days
        total=`div $total 24`
        days=`add $total $day`
        if [ $days -gt $daymax ]; then
           day=`sub $days $daymax`
           month=`add $month 1`
       else
         day=$days
       fi
       if [ $month -gt 12 ]; then
          year=`add $year 1`
          month=1
       fi  
     #"-"condition
     else
       #get total hours of off
       total=`div $off 60`
       #get hour part  of off in minute
       tmp=`mult $total  60`
       #get minute of off
       tmp=`sub $off $tmp`
       #cal minute part
       tmp=`sub $minute  $tmp`
       if [ $tmp -lt 0 ];then
          total=`add $total 1`
          minute=`add $tmp 60`
       else
          minute=$tmp
       fi
       #get total days of off
       tmp=$total
       total=`div $tmp 24`
       tmp1=`mult $total  24`
       tmp=`sub $tmp $tmp1`
       #cal hour part
       tmp=`sub  $hour $tmp`
       if [ $tmp -lt 0 ]; then
          total=`add $total 1`
          hour=`add $tmp 24`
       else
         hour=$tmp
       fi
       #deal days
       tmp=`sub $day $total`
       if [ $tmp -le 0 ]; then
           month=`sub $month 1`
               if [ $month -eq 0 ]; then
                   year=`sub $year 1`
                   if [ $year -lt 0 ]; then
                      prn can not cal BC
                      return 1
                   fi   
                  month=12
                  day=`add $tmp 31`
               else
                  tmp1=`leap $year $month`
                  day=`add $tmp $tmp1`
               fi
       else
           day=$tmp
       fi
     fi
     printf "%04d%02d%02d%02d%02d\n" $year $month $day $hour $minute
     return 0
  else   
     prn $info
     return 1
  fi
}
#cal time offset(计算偏移时间)

论坛徽章:
0
2 [报告]
发表于 2003-06-24 12:41 |只看该作者

日期偏移shell

论坛徽章:
0
3 [报告]
发表于 2003-06-24 14:48 |只看该作者

日期偏移shell

是优点晕.
主要完成什么功能?

论坛徽章:
0
4 [报告]
发表于 2003-06-24 14:59 |只看该作者

日期偏移shell

例如:dayoff -60 200306230000 (将计算2003年6月23零点零分,60分钟前是什么时间)运行得到结果为200306222300(2003年6月22日23点零分)
使用方法
#!/bin/sh
#上述shell函数包为shfunc
. ./shfunc
prn `dayoff "$1" "$2"`

论坛徽章:
0
5 [报告]
发表于 2003-06-24 15:42 |只看该作者

日期偏移shell


  1. #计算某时间前多少分钟是啥子时间
  2. #Usage:command yyyymmddHHMM times
  3. str=$1
  4. times=$2
  5. yy=`echo $str|cut -c 1-4`
  6. mm=`echo $str|cut -c 5-6`
  7. dd=`echo $str|cut -c 7-8`
  8. HH=`echo $str|cut -c 9-10`
  9. MM=`echo $str|cut -c 11-12`
  10. times=`expr $times - $MM`
  11. while [ $times -gt 0 ]
  12. do
  13.         HH=`expr $HH - 1`
  14.         [ $HH -eq -1 ] && HH=23 && dd=`expr $dd - 1`
  15.         if [ $dd -eq 0 ];then
  16.                 mm=`expr $mm - 1`
  17.                 if [ $mm -eq 0 ];then
  18.                         mm=12
  19.                         yy=`expr $yy - 1`
  20.                 fi
  21.                 aaa=`cal $mm $yy`
  22.                 dd=`echo $aaa|awk '{print $NF}'`
  23.         fi
  24.         times=`expr $times - 60`
  25. done
  26. MM=`expr 0 - $times`
  27. dd=`printf "%02d" $dd`
  28. mm=`printf "%02d" $mm`
  29. HH=`printf "%02d" $HH`
  30. MM=`printf "%02d" $MM`
  31. echo $yy$mm$dd$HH$MM

复制代码



pbc:/temp/ily/date>./dayoff 200401010000 60
200312312300
pbc:/temp/ily/date>./dayoff 200303010000 59
200302282301
pbc:/temp/ily/date>./dayoff 200401010000 1060
200312310620
pbc:/temp/ily/date>

论坛徽章:
0
6 [报告]
发表于 2003-06-24 15:56 |只看该作者

日期偏移shell

就是时间偏移啦用法是dayoff 偏移量(单位分钟) 基准时间,基准时间可以不写(默认为当前时间),偏移有向前和向后两种,某时间前多少分钟就是向前偏移,如计算2小时前是什么时间,(注意要把2小时转换成分钟)用法:dayoff -2880

论坛徽章:
0
7 [报告]
发表于 2003-06-24 16:12 |只看该作者

日期偏移shell

sorry是2天前,不是2小时前

论坛徽章:
0
8 [报告]
发表于 2003-06-24 16:19 |只看该作者

日期偏移shell

...

不错,就是看着有点晕~~~  

你在贴代码的时候可以用 code, 而不是用 quote,就会好多了

[code]
Your code here
[/code]

论坛徽章:
0
9 [报告]
发表于 2003-06-24 16:26 |只看该作者

日期偏移shell

红袖早起.

论坛徽章:
0
10 [报告]
发表于 2003-06-24 16:31 |只看该作者

日期偏移shell

...

蓝裤脚好, 不早啦,
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP