- 浏览: 4734487 次
- 性别:
- 来自: 上海
文章分类
最新评论
-
bzhao:
你也应该可以这样:(not tested)./rbtunnel ...
在Bash脚本中怎么关闭文件描述符? -
bzhao:
如果有A进程原代码情况下,通过如下调用,把他的子进程继承关闭则 ...
在Bash脚本中怎么关闭文件描述符? -
Master-Gao:
楼主咋没分析下源码呢?
我使用过的Linux命令之dirname - 截取给定路径的目录部分 -
jiedushi:
tail -F 就可以吧
Linux下实时跟踪log4j日志文件的bash脚本 - 增强了tail -f的功能 -
java_is_new:
新手学习了,就是不明白为一个网卡配多个ip有什么用
我使用过的Linux命令之ifconfig - 网络配置命令
在编写Linux Bash脚本时,经常会用到一些日期时间有关的命令,下面是我多年Shell编程中常用的函数,现在整理出来,希望起到抛砖引玉的作用。
附件包括三个文件:
datetime.sh 包含了Bash中关于日期时间操作的常用自定义函数
test_datetime.sh 用来展示datetime.sh中自定义函数的用法
test_datetime.txt 是test_datetime.sh的一次执行输出样本
执行命令:
./test_datetime.sh >test_datetime.txt
文件:datetime.sh
#!/bin/sh # Copyright (c) 2010 codingstandards. All rights reserved. # file: datetime.sh # description: Bash中关于日期时间操作的常用自定义函数 # license: LGPL # author: codingstandards # email: codingstandards@gmail.com # version: 1.0 # date: 2010.02.27 # usage: yesterday # 昨天 # 比如今天是2010年2月27日,那么结果就是2010-02-26 yesterday() { date --date='1 day ago' +%Y-%m-%d } # usage: today # 今天 # 比如今天是2010年2月27日,那么结果就是2010-02-27 today() { date +%Y-%m-%d } # usage: now # 现在,包括日期和时间、纳秒 # 比如:2010-02-27 11:29:52.991774000 now() { date "+%Y-%m-%d %H:%M:%S.%N" } # usage: curtime # 当前时间,包括日期和时间 # 比如:2010-02-27 11:51:04 curtime() { date '+%Y-%m-%d %H:%M:%S' # 也可写成:date '+%F %T' } # usage: last_month # 取上个月的年月 # 比如:2010-01 last_month() { date --date='1 month ago' '+%Y-%m' } # usage: last_month_packed # 取上个月的年月 # 比如:201001 last_month_packed() { date --date='1 month ago' '+%Y%m' } # usage: first_date_of_last_month # 取上个月的第一天 # 比如本月是2010年2月,那么结果就是2010-01-01 first_date_of_last_month() { date --date='1 month ago' '+%Y-%m-01' } # usage: last_date_of_last_month # 取上个月的最后一天 # 比如当前是2010年2月,那么结果就是2010-01-31 last_date_of_last_month() { date --date="$(date +%e) days ago" '+%Y-%m-%d' } # usage: day_of_week # 今天的星期 # day of week (0..6); 0 represents Sunday day_of_week() { date +%w } # usage: last_hour # 上个小时 # 比如:2010-02-27-10 # 适合处理log4j生成的日志文件名 last_hour() { date --date='1 hour ago' +%Y-%m-%d-%H } # usage: the_hour # 当前的小时,为方便算术比较,结果不以0开头 # 比如:12 the_hour() { #date +%H # hour (00..23) date +%k # hour ( 0..23) } # usage: the_minute # 当前的分钟,为方便算术比较,结果不以0开头 # 比如: the_minute() { MM=$(date +%M) # minute (00..59) echo $[1$MM-100] } # usage: the_second # 当前的秒数 # 比如: the_second() { SS=$(date +%S) # second (00..60); the 60 is necessary to accommodate a leap second echo $[1$SS-100] } # usage: the_year # 当前的年份 year (1970...) # 比如:2010 the_year() { date +%Y } # usage: the_month # 当前的月份,为方便算术比较,结果不以0开头 # 比如:2 the_month() { M=$(date +%m) # month (01..12) echo $[1$M-100] } # usage: the_date # 当前的日期,为方便算术比较,结果不以0开头 # 比如:27 the_date() { date +%e # day of month, blank padded ( 1..31) } # usage: days_ago <n> # 取n天前的日期 # 比如:days_ago 0就是今天,days_ago 1就是昨天,days_ago 2就是前天,days_ago -1就是明天 # 格式:2010-02-27 days_ago() { date --date="$1 days ago" +%Y-%m-%d } # usage: chinese_date_and_week() # 打印中文的日期和星期 # 比如:2月27日 星期六 chinese_date_and_week() { WEEKDAYS=(星期日 星期一 星期二 星期三 星期四 星期五 星期六) WEEKDAY=$(date +%w) #DT="$(date +%Y年%m月%d日) ${WEEKDAYS[$WEEKDAY]}" MN=1$(date +%m) MN=$[MN-100] DN=1$(date +%d) DN=$[DN-100] DT="$MN月$DN日 ${WEEKDAYS[$WEEKDAY]}" echo "$DT" } # usage: rand_digit # 随机数字,0-9 rand_digit() { S="$(date +%N)" echo "${S:5:1}" } # usage: seconds_of_date [<date> [<time>]] # 获取指定日期的秒数(自1970年) # 比如:seconds_of_date "2010-02-27" 返回 1267200000 seconds_of_date() { if [ "$1" ]; then date -d "$1 $2" +%s else date +%s fi } # usage: date_of_seconds <seconds> # 根据秒数(自1970年)得到日期 # 比如:date_of_seconds 1267200000 返回 2010-02-27 date_of_seconds() { date -d "1970-01-01 UTC $1 seconds" "+%Y-%m-%d" } # usage: datetime_of_seconds <seconds> # 根据秒数(自1970年)得到日期时间 # 比如:datetime_of_seconds 1267257201 返回 2010-02-27 15:53:21 datetime_of_seconds() { date -d "1970-01-01 UTC $1 seconds" "+%Y-%m-%d %H:%M:%S" } # usage: leap_year <yyyy> # 判断是否闰年 # 如果yyyy是闰年,退出码为0;否则非0 # 典型示例如下: # if leap_year 2010; then # echo "2010 is leap year"; # fi # if leap_year 2008; then # echo "2008 is leap year"; # fi # 摘自脚本:datetime_util.sh (2007.06.11) # 注:这个脚本来自网络,略有修改(原脚本从标准输入获取年份,现改成通过参数指定) # Shell program to read any year and find whether leap year or not # ----------------------------------------------- # Copyright (c) 2005 nixCraft project <http://cyberciti.biz/fb/> # This script is licensed under GNU GPL version 2.0 or above # ------------------------------------------------------------------------- # This script is part of nixCraft shell script collection (NSSC) # Visit http://bash.cyberciti.biz/ for more information. # ------------------------------------------------------------------------- leap_year() { # store year yy=$1 isleap="false" #echo -n "Enter year (yyyy) : " #read yy # find out if it is a leap year or not if [ $((yy % 4)) -ne 0 ] ; then : # not a leap year : means do nothing and use old value of isleap elif [ $((yy % 400)) -eq 0 ] ; then # yes, it's a leap year isleap="true" elif [ $((yy % 100)) -eq 0 ] ; then : # not a leap year do nothing and use old value of isleap else # it is a leap year isleap="true" fi #echo $isleap if [ "$isleap" == "true" ]; then # echo "$yy is leap year" return 0 else # echo "$yy is NOT leap year" return 1 fi } # usage: validity_of_date <yyyy> <mm> <dd> # 判断yyyy-mm-dd是否合法的日期 # 如果是,退出码为0;否则非0 # 典型示例如下: # if validity_of_date 2007 02 03; then # echo "2007 02 03 is valid date" # fi # if validity_of_date 2007 02 28; then # echo "2007 02 28 is valid date" # fi # if validity_of_date 2007 02 29; then # echo "2007 02 29 is valid date" # fi # if validity_of_date 2007 03 00; then # echo "2007 03 00 is valid date" # fi # 摘自脚本:datetime_util.sh (2007.06.11) # 注:这个脚本来自网络,略有修改(原脚本从标准输入获取年月日,现改成通过参数指定) # Shell program to find the validity of a given date # ----------------------------------------------- # Copyright (c) 2005 nixCraft project <http://cyberciti.biz/fb/> # This script is licensed under GNU GPL version 2.0 or above # ------------------------------------------------------------------------- # This script is part of nixCraft shell script collection (NSSC) # Visit http://bash.cyberciti.biz/ for more information. # ------------------------------------------------------------------------- validity_of_date() { # store day, month and year yy=$1 mm=$2 dd=$3 # store number of days in a month days=0 # get day, month and year #echo -n "Enter day (dd) : " #read dd #echo -n "Enter month (mm) : " #read mm #echo -n "Enter year (yyyy) : " #read yy # if month is negative (<0) or greater than 12 # then it is invalid month if [ $mm -le 0 -o $mm -gt 12 ]; then #echo "$mm is invalid month." return 1 fi # Find out number of days in given month case $mm in 1) days=31;; 01) days=31;; 2) days=28 ;; 02) days=28 ;; 3) days=31 ;; 03) days=31 ;; 4) days=30 ;; 04) days=30 ;; 5) days=31 ;; 05) days=31 ;; 6) days=30 ;; 06) days=30 ;; 7) days=31 ;; 07) days=31 ;; 8) days=31 ;; 08) days=31 ;; 9) days=30 ;; 09) days=30 ;; 10) days=31 ;; 11) days=30 ;; 12) days=31 ;; *) days=-1;; esac # find out if it is a leap year or not if [ $mm -eq 2 ]; then # if it is feb month then only check of leap year if [ $((yy % 4)) -ne 0 ] ; then : # not a leap year : means do nothing and use old value of days elif [ $((yy % 400)) -eq 0 ] ; then # yes, it's a leap year days=29 elif [ $((yy % 100)) -eq 0 ] ; then : # not a leap year do nothing and use old value of days else # it is a leap year days=29 fi fi #echo $days # if day is negative (<0) and if day is more than # that months days then day is invaild if [ $dd -le 0 -o $dd -gt $days ]; then #echo "$dd day is invalid" return 3 fi # if no error that means date dd/mm/yyyy is valid one #echo "$dd/$mm/$yy is a vaild date" #echo "$yy-$mm-$dd is a valid date" #echo "valid" return 0 } # usage: days_of_month <mm> <yyyy> # 获取yyyy年mm月的天数,注意参数顺序 # 比如:days_of_month 2 2007 结果是28 days_of_month() { # store day, month and year mm=$1 yy=$2 # store number of days in a month days=0 # get day, month and year #echo -n "Enter day (dd) : " #read dd #echo -n "Enter month (mm) : " #read mm #echo -n "Enter year (yyyy) : " #read yy # if month is negative (<0) or greater than 12 # then it is invalid month if [ $mm -le 0 -o $mm -gt 12 ]; then #echo "$mm is invalid month." echo -1 return 1 fi # Find out number of days in given month case $mm in 1) days=31;; 01) days=31;; 2) days=28 ;; 02) days=28 ;; 3) days=31 ;; 03) days=31 ;; 4) days=30 ;; 04) days=30 ;; 5) days=31 ;; 05) days=31 ;; 6) days=30 ;; 06) days=30 ;; 7) days=31 ;; 07) days=31 ;; 8) days=31 ;; 08) days=31 ;; 9) days=30 ;; 09) days=30 ;; 10) days=31 ;; 11) days=30 ;; 12) days=31 ;; *) days=-1;; esac # find out if it is a leap year or not if [ $mm -eq 2 ]; then # if it is feb month then only check of leap year if [ $((yy % 4)) -ne 0 ] ; then : # not a leap year : means do nothing and use old value of days elif [ $((yy % 400)) -eq 0 ] ; then # yes, it's a leap year days=29 elif [ $((yy % 100)) -eq 0 ] ; then : # not a leap year do nothing and use old value of days else # it is a leap year days=29 fi fi echo $days }
文件:test_datetime.sh
#!/bin/sh # TODO: 注意根据datetime.sh的实际位置更改 . /opt/shtools/commons/datetime.sh echo "当前时间(date):$(date)" echo "昨天(yesterday):$(yesterday)" echo "今天(today):$(today)" echo "现在(now):$(now)" echo "现在(curtime):$(curtime)" echo "上月(last_month):$(last_month)" echo "上月(last_month_packed):$(last_month_packed)" echo "上月第一天(first_date_of_last_month):$(first_date_of_last_month)" echo "上月最后一天(last_date_of_last_month):$(last_date_of_last_month)" echo "今天星期几(day_of_week):$(day_of_week)" echo "上个小时(last_hour):$(last_hour)" echo "当前的小时(the_hour):$(the_hour)" echo "当前的分钟(the_minute):$(the_minute)" echo "当前的秒钟(the_second):$(the_second)" echo "当前的年份(the_year):$(the_year)" echo "当前的月份(the_month):$(the_month)" echo "当前的日期(the_date):$(the_date)" echo "前天(days_ago 2):$(days_ago 2)" echo "明天(days_ago -1):$(days_ago -1)" echo "后天(days_ago -2):$(days_ago -2)" echo "十天前的日期(days_ago 10):$(days_ago 10)" echo "中文的日期星期(chinese_date_and_week):$(chinese_date_and_week)" echo "随机数字(rand_digit):$(rand_digit)" echo "随机数字(rand_digit):$(rand_digit)" echo "自1970年来的秒数(seconds_of_date):$(seconds_of_date)" echo "自1970年来的秒数(seconds_of_date 2010-02-27):$(seconds_of_date 2010-02-27)" echo "自1970年来的秒数(seconds_of_date 2010-02-27 15:53:21):$(seconds_of_date 2010-02-27 15:53:21)" echo "自1970年来的秒数对应的日期(date_of_seconds 1267200000):$(date_of_seconds 1267200000)" echo "自1970年来的秒数对应的日期时间(datetime_of_seconds 1267257201):$(datetime_of_seconds 1267257201)" if leap_year 2010; then echo "2010年是闰年"; fi if leap_year 2008; then echo "2008年是闰年"; fi if validity_of_date 2007 02 03; then echo "2007 02 03 日期合法" fi if validity_of_date 2007 02 28; then echo "2007 02 28 日期合法" fi if validity_of_date 2007 02 29; then echo "2007 02 29 日期合法" fi if validity_of_date 2007 03 00; then echo "2007 03 00 日期合法" fi echo "2010年2月的天数(days_of_month 2 2010):$(days_of_month 2 2010)" echo "2008年2月的天数(days_of_month 2 2008):$(days_of_month 2 2008)"
文件:test_datetime.txt
当前时间(date):六 2月 27 15:58:28 CST 2010 昨天(yesterday):2010-02-26 今天(today):2010-02-27 现在(now):2010-02-27 15:58:28.734817000 现在(curtime):2010-02-27 15:58:28 上月(last_month):2010-01 上月(last_month_packed):201001 上月第一天(first_date_of_last_month):2010-01-01 上月最后一天(last_date_of_last_month):2010-01-31 今天星期几(day_of_week):6 上个小时(last_hour):2010-02-27-14 当前的小时(the_hour):15 当前的分钟(the_minute):58 当前的秒钟(the_second):28 当前的年份(the_year):2010 当前的月份(the_month):2 当前的日期(the_date):27 前天(days_ago 2):2010-02-25 明天(days_ago -1):2010-02-28 后天(days_ago -2):2010-03-01 十天前的日期(days_ago 10):2010-02-17 中文的日期星期(chinese_date_and_week):2月27日 星期六 随机数字(rand_digit):5 随机数字(rand_digit):9 自1970年来的秒数(seconds_of_date):1267257508 自1970年来的秒数(seconds_of_date 2010-02-27):1267200000 自1970年来的秒数(seconds_of_date 2010-02-27 15:53:21):1267257201 自1970年来的秒数对应的日期(date_of_seconds 1267200000):2010-02-27 自1970年来的秒数对应的日期时间(datetime_of_seconds 1267257201):2010-02-27 15:53:21 2008年是闰年 2007 02 03 日期合法 2007 02 28 日期合法 2010年2月的天数(days_of_month 2 2010):28 2008年2月的天数(days_of_month 2 2008):29
- datetime.rar (4.2 KB)
- 下载次数: 104
发表评论
-
在Linux下将整个目录的文件转换字符编码的脚本
2012-06-18 20:47 3585源文件编码:gbk 源文件名称模式:*.ftl.gbk 目 ... -
Bash小技巧(惯用法)-2
2012-05-12 07:17 2155读取文件的第一行 很简单: head -1 file.tx ... -
Bash小技巧(惯用法)-1
2012-05-10 20:55 2575Bash小技巧(惯用法) 退出码判断(命令执行成功与否的判断 ... -
写一个Bash脚本来计算母亲节和父亲节的日期(推荐)
2012-05-10 20:31 2756写一个Bash脚本来计算母亲节和父亲节的日期 母亲节(每年 ... -
推荐Bash提高资料:写出健壮的Bash脚本
2012-03-20 13:58 3423写出健壮的Bash脚本 本文链接:http://coding ... -
在Linux下使用top命令查看所有java进程的状态
2011-10-28 11:09 9629在Linux下使用top命令查 ... -
导入MySQL数据库模式及数据的Bash脚本 (导出的姊妹篇)
2011-10-11 09:48 2778导入MySQL数据库模式及数据的Bash脚本 本文链接:ht ... -
导出MySQL数据库模式及数据的Bash脚本 真的很好用!
2011-10-10 10:14 3929导出MySQL数据库模式及数据的Bash脚本 本文链 ... -
导出MySQL数据库模式及数据的Bash脚本
2011-10-08 14:41 0导出MySQL数据库模式及数据的Bash脚本 ... -
AA制消费金额统计的Bash脚本
2011-09-02 19:57 2033AA制消费金额统计的Bash ... -
AA制消费金额统计的Bash脚本
2011-09-02 16:27 0AA制消费金额统计的Bash ... -
Bash字符串处理总结(与Java对比)
2011-08-26 22:02 0。 Bash字符串处理总结(与Java对比) ... -
Linux下实时跟踪log4j日志文件的bash脚本 - 增强了tail -f的功能
2010-11-05 13:08 7668Linux下实时跟踪log4j日志文件的bash脚本 - 增强 ... -
用Google AJAX Search API对互联网上Linux命令出现次数排名
2010-10-31 14:30 3171用Google AJAX Search API对互联网上Lin ... -
Linux下把多个子目录中的源代码添加到CVS仓库的Bash脚本
2010-10-23 15:19 3435Linux下把多个子目录中的源代码添加到CVS仓库的Bash脚 ... -
我使用过的Linux命令之history
2010-09-30 14:03 0我使用过的Linux命令之history 本文链接: ( ... -
我使用过的Linux命令之g++
2010-09-29 13:51 0我使用过的Linux命令之g++ 用途说明 常用参数 使 ... -
我使用过的Linux命令之gzip
2010-09-29 13:51 0我使用过的Linux命令之gzip 用途说明 常用参数 ... -
我使用过的Linux命令之gunzip
2010-09-29 13:50 0我使用过的Linux命令之gunzip 用途说明 常用参数 ... -
我使用过的Linux命令之groups
2010-09-29 13:50 0我使用过的Linux命令之groups 用途说明 常用参数 ...
相关推荐
bash写的字符串常用函数,这个可以看看。资源分就免了
Git Bash常用操作命令,包括基本的pull push merge操作等
gittify - 一个多彩的Bash提示符 自定义的Git别名
学习git和gitbash使用的一些详细操作,学习git和gitbash使用的一些详细操作
bash-lambda, bash的匿名函数和FP内容 Bash注意:由于性能原因,由于切换到 zsh,我不再积极维护这个项目。 我可以能会编写类似于wince的类似的内容,但是很希望自动散列支持更快。对 bash ( 一个功能完备的黑客)的...
BASH中文手册.pdfBASH中文手册.pdf
自定义 .bash_profile 自定义终端的外观。 在您的机器上导航时,它可以简单地读出您的身份和位置。 大多数着色是从其他地方获取的,但我真的不知道从哪里得到代码,因为我从朋友那里得到了代码。 如果有人知道,我...
本文档是自己总结的关于 bash shell 的字符串的操作合集, 并生成了可以复制内容的pdf 以保持排版
Bash中文手册,适合Linux shell入门级使用!
Git Bash 常用的终端命令 .
中国linux论坛man手册页翻译计划,bash的中文解释,翻译的很不错。对于学习linux脚本编程的同学来说,绝对是不可以没有的。word文档格式。
添加您的自定义Bash函数 将bash函数追加到~/.dotfiles/.bash_functions 添加您的自定义Bash别名 在~/.dotfiles/.bash_aliases追加bash别名 添加您的自定义Bash命令 要添加bash命令,请将命令的文件夹/脚本移动到~/....
Git Bash常用命令与解释
本资源为二狗自己编写的快速定义java环境变量的脚本,可以帮助你剩下不少时间,操作前建议备份/etc/profile文件:mv /etc/profile{,.bak} 防止脚本问题覆盖掉文件的内容 资源分为脚本和安装包两部分内容,放置在同一...
前言 ...在 Linux 中自定义 bash 命令提示符 在 bash 中,我们可以通过更改 $PS1 环境变量的值来自定义 bash 命令提示符。 一般情况下,bash 命令提示符会是以下这样的形式: 在上图这种默认显示形
bash-fun: bash中的函数式编程
主要介绍了Linux Bash 提示符的一些骚操作,一些能让你自定义 Bash 提示符的黑科技,需要的朋友可以参考下
thunar-custom-actions, Thunar ( 或者 Nautilus ) 文件管理器的[sh,bash] 自定义操作 Thunar自定义操作 以下操作也可以用于nautilus或者支持自定义操作的任何其他文件管理器。 我个人更喜欢Thunar因为速度。如果...
Bash的中文文档,其中有错别字以及漏字的地方,请大家通过上下文来进行理解。