`
ganliang13
  • 浏览: 249440 次
  • 性别: Icon_minigender_1
  • 来自: 北京
社区版块
存档分类
最新评论

shell 年月日 处理

阅读更多
!/bin/bash
get_mon_days()
{
 Y=`expr substr $1 1 4`
 M=`expr substr $1 5 2`

 r1=`expr $Y \% 4`
 r2=`expr $Y \% 100`
 r3=`expr $Y \% 400`

 case $M in
 01|03|05|07|08|10|12) days=31;;
 04|06|09|11) days=30;;
 02)
  r1=`expr $Y \% 4`
  r2=`expr $Y \% 100`
  r3=`expr $Y \% 400`
  # 闰年
  if [ r1 -eq 0 -a r2 -ne 0 -o r3 -eq 0  ] ; then
   days=29
  else
   days=28
  fi
  ;;
 *)
  days=0
  ;;
 esac
 echo $days
}

check_date()
{
 # 0 合法 1 非法
 FLAG=0
 Y=`expr substr $1 1 4`
 M=`expr substr $1 5 2`
 D=`expr substr $1 7 2`

 days=`get_mon_days $Y$M`

 if ! [ $D -ge 1 -a $D -le $days ] ; then
  FLAG=1
 fi
 return $FLAG
}

get_before_date()
{
 Y=`expr substr $1 1 4`
 M=`expr substr $1 5 2`
 D=`expr substr $1 7 2`
 YY=`expr $Y - 1`
 MM=`expr $M - 1`
 DD=`expr $D - 1`
 MM=`printf "%02d" $MM`
 DD=`printf "%02d" $DD`
 dd=$Y$MM
 dad=`get_mon_days $dd`
 be_date=$Y$M$DD
 if [ $D -eq 01 ]
 then
  if [ $M -ne 01 ]
  then
   be_date=$Y$MM$dad
  fi
  if [ $M -eq 01 ]
  then
   be_date=$YY"1231"
  fi
 fi
 echo $be_date
}

get_next_date()
{
 Y=`expr substr $1 1 4`
 M=`expr substr $1 5 2`
 D=`expr substr $1 7 2`
 YY=`expr $Y + 1`
 MM=`expr $M + 1`
 DD=`expr $D + 1`
 MM=`printf "%02d" $MM`
 DD=`printf "%02d" $DD`
 r1=`expr $Y \% 4`
 r2=`expr $Y \% 100`
 r3=`expr $Y \% 400`

 # 通常情况
 next_date=$Y$M$DD

 # 特殊情况1,小月月末
 if [ $D -eq 30 ]
 then
  case $M in
  04|06|09|11) next_date=$Y$MM"01";;
  esac
 fi
 # 特殊情况2,大月月末
 if [ $D -eq 31 ]
 then
  next_date=$Y$MM"01"
  case $M in
   12) next_date=$YY"0101";;
  esac
 fi
 # 特殊情况3,2月月末
 if [ $M -eq 02 ]
 then
  # 闰年
  if [ r1 -eq 0 -a r2 -ne 0 -o r3 -eq 0 ]
  then
   if [ $D -eq 29 ]
   then
    next_date=$Y$MM"01"
   fi
  else
   if [ $D -eq 28 ]
   then
    next_date=$Y$MM"01"
   fi
  fi
 fi
 echo $next_date
}

format_date()
{
 Y=`expr substr $1 1 4`
 M=`expr substr $1 5 2`
 D=`expr substr $1 7 2`
 echo $Y"-"$M"-"$D
}

if [ $# -ne 1 ] 
then    
    n_today=$(date -d "0 days ago" +%Y%m%d)
elif check_date $1
then
    n_today=$1
else
    n_today=$(date -d "0 days ago" +%Y%m%d)
fi
v_yesterday=$(format_date $(get_before_date $n_today))
v_today=$(format_date $n_today)
n_yesterday=$(get_before_date $n_today)

echo $n_yesterday
echo $n_today
echo $v_yesterday
echo $v_today
 
 

 

分享到:
评论

相关推荐

    shell字符串处理

    在做shell批处理程序时候,经常会涉及到字符串相关操作。有很多命令语句,如:awk,sed都可以做字符串各种操作。 其实shell内置一系列操作符号,可以达到类似效果,大家知道,使用内部操作符会省略启动外部程序等时间...

    shell 定时任务处理

    shell 定时任务处理

    shell日期处理函数

    附件是shell的日期处理函数,包含大多数的日期处理方法,希望对大家有帮助。

    shell中长命令的换行处理方法示例

    主要给大家介绍了关于shell中长命令的换行处理方法,文中通过示例代码介绍的非常详细,对大家学习或者使用shell具有一定的参考学习价值,需要的朋友们下面来一起学习学习吧

    在Shell命令行处理JSON数据的方法

    因为最近要处理一些 JSON 数据格式,所以在经过一番搜索后 最终找到了 jq 这个很棒的工具。jq 允许你直接在命令行下对 JSON 进行操作,包括分片、过滤、转换等等。 让我们通过几个例子来说明 jq 的功能: 一、输出...

    B shell与 C shell的区别

    B shell与 C shell的区别 ; B shell的详解和C shell 的详解

    shell shell练习 shell入门

    shell shell练习 shell入门 shell shell练习 shell入门

    Dialog设计,关于shell 脚本的处理

    Dialog设计,关于shell 脚本的处理, dialog是个shell scripts用的,事实上当您下载Linux Kernel时,里面有个 scripts/lxdialog目录,其实那就是dialog原始码,只是Linux kernel为了避 免与原有dialog相冲突,...

    shell 处理apache日志入库

    shell 处理apache日志入库 awk分析apache日志为*.sql文件 sqlplus写入oracle

    shell 批量处理 字符串指令行

    NULL 博文链接:https://annan211.iteye.com/blog/2412844

    shell中字符串处理方

    shell中字符串处理方式,Linux下灵活活处理字符串的说明。。

    shell讲义shell讲义

    shell讲义shell讲义shell讲义shell讲义shell讲义shell讲义shell讲义shell讲义shell讲义shell讲义shell讲义shell讲义shell讲义shell讲义shell讲义shell讲义shell讲义shell讲义shell讲义shell讲义shell讲义shell讲义...

    shell算法shell算法shell算法

    shell排序shell排序shell排序shell排序shell排序shell排序shell排序

    shell和bat处理sql脚本

    shell和bat处理sql脚本

    Shell源码(Shell源码)

    Shell源码Shell源码Shell源码Shell源码Shell源码Shell源码Shell源码Shell源码Shell源码Shell源码

    Shell处理带空格的文件名的方法

    但是这种命名方式给Linux命令行工具和Shell带来了困扰,因为大多数命令中,都是默认以空格做为值与值之间的分隔符,而不是做为文件名的一部分。 看下面的脚本,我们可以了解的更清楚: 执行的结果如下: 实际上,...

    1_shell_shell处理HTML_picturedjj4_

    关于shell中长命令的换行处理方法组织了很多参数。整个命令都在一行之中,不是很好阅读和维护。

    实验一shell实验报告

    实验1: SHELL编程 实验目的 熟悉linux常用命令,为以后的实验打下良好...$mycal [月份名] 年时,屏幕输出指定的年月的月历。例如: $mycal October 2009 屏幕输出2009年10月的月历。 注:参数可以是英文或者是数字。

    shell 编程shell 编程

    shell 编程shell 编程shell 编程shell 编程shell 编程shell 编程shell 编程shell 编程shell 编程shell 编程shell 编程shell 编程shell 编程shell 编程shell 编程shell 编程shell 编程shell 编程shell 编程shell 编程...

    shell高级编程 shell的经典教程

    shell 通过提示您输入,向操作系统解释该输入,然后处理来自操作系统的任何结果输出来管理您与操作系统之间的交互。  shell 向您提供了与操作系统通信的方式。此通信以交互的方式(来自键盘的输入立即操作)或作为...

Global site tag (gtag.js) - Google Analytics