设为首页 收藏本站
查看: 732|回复: 0

shell编程中的date用法

[复制链接]

尚未签到

发表于 2015-4-29 06:31:53 | 显示全部楼层 |阅读模式
  最近开始染指linux,基本操作,尤其是关于目录、文件系统磁盘管理+进程这两部分做了一些了解,随后开始学习下shell编程。
  在网上见到关于shell的date使用总结,先存下来留待以后使用。
  http://blog.sina.com.cn/s/blog_61c006ea0100mgxe.html

  1、date --help
  %% 输出%符号 a literal %
%a 当前域的星期缩写 locale’s abbreviated weekday name (Sun..Sat)
%A 当前域的星期全写 locale’s full weekday name, variable length(Sunday..Saturday)
%b 当前域的月份缩写 locale’s abbreviated month name (Jan..Dec)
%B 当前域的月份全称 locale’s full month name, variable length(January..December)
%c 当前域的默认时间格式 locale’s date and time (Sat Nov 04 12:02:33 EST1989)
%C n百年 century (year divided by 100 and truncated to an integer)[00-99]
%d 两位的天 day of month (01..31)
%D 短时间格式 date (mm/dd/yy)
%e 短格式天 day of month, blank padded ( 1..31)
%F 文件时间格式 same as %Y-%m-%d
%g the 2-digit year corresponding to the %V week number
%G the 4-digit year corresponding to the %V week number
%h same as %b
%H 24小时制的小时 hour (00..23)
%I 12小时制的小时 hour (01..12)
%j 一年中的第几天 day of year (001..366)
%k 短格式24小时制的小时 hour ( 0..23)
%l 短格式12小时制的小时 hour ( 1..12)
%m 双位月份 month (01..12)
%M 双位分钟 minute (00..59)
%n 换行 a newline
%N 十亿分之一秒 nanoseconds (000000000..999999999)
%p 大写的当前域的上下午指示 locale’s upper case AM or PM indicator (blank inmany locales)
%P 小写的当前域的上下午指示 locale’s lower case am or pm indicator (blank inmany locales)
%r 12小时制的时间表示(时:分:秒,双位) time, 12-hour (hh:mm:ss [AP]M)
%R 24小时制的时间表示 (时:分,双位)time, 24-hour (hh:mm)
%s 自基础时间 1970-01-01 00:00:00 到当前时刻的秒数 seconds since `00:00:001970-01-01 UTC’ (a GNU extension)
%S 双位秒 second (00..60); the 60 is necessary to accommodate a leapsecond
%t 横向制表位(tab) a horizontal tab
%T 24小时制时间表示 time, 24-hour (hh:mm:ss)
%u 数字表示的星期(从星期一开始 1-7)day of week (1..7); 1 represents Monday
%U 一年中的第几周星期天为开始 week number of year with Sunday as first day ofweek (00..53)
%V 一年中的第几周星期一为开始 week number of year with Monday as first day ofweek (01..53)
%w 一周中的第几天 星期天为开始 0-6 day of week (0..6); 0 represents Sunday
%W 一年中的第几周星期一为开始 week number of year with Monday as first day ofweek (00..53)
%x 本地日期格式 locale’s date representation (mm/dd/yy)
%X 本地时间格式 locale’s time representation (%H:%M:%S)
%y 两位的年 last two digits of year (00..99)
%Y 年 year (1970…)
%z RFC-2822 标准时间格式表示的域 RFC-2822 style numeric timezone (-0500) (anonstandard extension)
%Z 时间域 time zone (e.g., EDT), or nothing if no time zone isdeterminable
  By default, date pads numeric fields with zeroes. GNU daterecognizes
the following modifiers between `%’ and a numeric directive.
  `-’ (hyphen) do not pad the field
`_’ (underscore) pad the field with spaces
  --------------------------------------------------------------------------------
  2、一些用法
  1)#以yymmdd的格式输出43天前的当前时刻
  date +%Y%m%d --date='43 daysago'      
  
  2)# 测试十亿分之一秒
date +’%Y%m%d %H:%M:%S.%N’;date +’%Y%m%d %H:%M:%S.%N’;date +’%Y%m%d%H:%M:%S.%N’;date +’%Y%m%d %H:%M:%S.%N’
  3)#创建以当前时间为文件名的目录
mkdir `date +%Y%m%d`
  
  4)#备份以时间做为文件名的
tar -cvf ./htdocs`date +%Y%m%d`.tar ./*
  
  5)#显示时间后跳行,再显示目前日期
  date +%T%n%Y%m%d
  
  6)#只显示月份与日数
  date +%B%d
  
  7)#获取上周日期(day,month,year,hour)
  date -d "-1 week" +%Y%m%d   
  
  8)#获取24小时前日期
  date --date="-24 hour" +%Y%m%d
  
  9)#shell脚本里面赋给变量值
  date_now=`date +%s`
  
  10)#计算执行一段sql脚本的运行时间
  
  TIME_BEGIN=$(date '+%s.%N')
$sqlcli < queries/q1.3.sql1>> $FILE_RESULT 2>> $FILE_ERROR
TIME_END=$(date '+%s.%N')
TIME_RUN=$(awk 'BEGIN{print '$TIME_END' - '$TIME_BEGIN'}')
  
  11)#编写shell脚本计算离自己生日还有多少天?
  read -p"Input your birthday(YYYYmmdd):" date1
  m=`date --date="$date1"+%m`   #得到生日的月
  d=`date --date="$date1"+%d`   #得到生日的日
  date_now=`date+%s`            #得到当前时间的秒值
  y=`date+%Y`                    #得到当前时间的年
  birth=`date --date="$y$m$d"+%s`     #得到今年的生日日期的秒值
  internal=$(($birth-$date_now))       #计算今日到生日日期的间隔时间
  if [ "$internal" -lt "0" ];then            #判断今天的生日是否已过
  birth=`date --date="$(($y+1))$m$d"+%s`     #得到明天的生日日期秒值
  internal=$(($birth-$date_now))              #计算今天到下一个生日的间隔时间
  fi
  echo "There is :$((einternal/60/60/24))days."      #输出结果,秒换算为天
  
  
  12)#若是不以加号作为开头,则表示要设定时间,而时间格式为 MMDDhhmm[[CC]YY][.ss],
  其中 MM 为月份,
  DD 为日,
  hh 为小时,
  mm 为分钟,
  CC 为年份前两位数字,
  YY 为年份后两位数字,
  ss 为秒数
  
  13)
  #显示目前的格林威治时间,也叫“世界时”。是英国的标准时间,也是世界各地时间的参考标准。中英两国的标准时差为8个小时,即英国的当地时间比中国的北京时间晚8小时。
  date-u              
Thu Sep 28 09:32:04 UTC 2006
  
  14)#修改时间
  date -s
按字符串方式修改时间
可以只修改日期,不修改时间,输入: date -s 2007-08-03
只修改时间,输入:date -s 14:15:00
同时修改日期时间,注意要加双引号,日期与时间之间有一空格,输入:date -s "2007-08-03 14:15:00"

修改完后,记得输入:clock -w
把系统时间写入CMOS

运维网声明 1、欢迎大家加入本站运维交流群:群②:261659950 群⑤:202807635 群⑦870801961 群⑧679858003
2、本站所有主题由该帖子作者发表,该帖子作者与运维网享有帖子相关版权
3、所有作品的著作权均归原作者享有,请您和我们一样尊重他人的著作权等合法权益。如果您对作品感到满意,请购买正版
4、禁止制作、复制、发布和传播具有反动、淫秽、色情、暴力、凶杀等内容的信息,一经发现立即删除。若您因此触犯法律,一切后果自负,我们对此不承担任何责任
5、所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其内容的准确性、可靠性、正当性、安全性、合法性等负责,亦不承担任何法律责任
6、所有作品仅供您个人学习、研究或欣赏,不得用于商业或者其他用途,否则,一切后果均由您自己承担,我们对此不承担任何法律责任
7、如涉及侵犯版权等问题,请您及时通知我们,我们将立即采取措施予以解决
8、联系人Email:admin@iyunv.com 网址:www.yunweiku.com

所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其承担任何法律责任,如涉及侵犯版权等问题,请您及时通知我们,我们将立即处理,联系人Email:kefu@iyunv.com,QQ:1061981298 本贴地址:https://www.yunweiku.com/thread-61641-1-1.html 上篇帖子: bash shell if 命令参数说明 下篇帖子: Shell脚本学习笔记(七)--sed和awk
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

扫码加入运维网微信交流群X

扫码加入运维网微信交流群

扫描二维码加入运维网微信交流群,最新一手资源尽在官方微信交流群!快快加入我们吧...

扫描微信二维码查看详情

客服E-mail:kefu@iyunv.com 客服QQ:1061981298


QQ群⑦:运维网交流群⑦ QQ群⑧:运维网交流群⑧ k8s群:运维网kubernetes交流群


提醒:禁止发布任何违反国家法律、法规的言论与图片等内容;本站内容均来自个人观点与网络等信息,非本站认同之观点.


本站大部分资源是网友从网上搜集分享而来,其版权均归原作者及其网站所有,我们尊重他人的合法权益,如有内容侵犯您的合法权益,请及时与我们联系进行核实删除!



合作伙伴: 青云cloud

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