AIX环境下计算昨天日期的shell两例
2010-08-04 15:50
344 查看
1、 改变时区的方法
cat yesterday.sh
2、 遍历所有情况类似函数的方法
cat yesterday.sh
说明:方法1较简洁明了,代码清晰;方法2是一般写程序的方法。
3、 [b]2中方法的进一步更新(支持20110101格式,即,年月固定用两位数)[/b]
cat yesterday.sh
说明:经过小小的改变,对实际应用提供了极大的方便。
cat yesterday.sh
aaa=`echo $TZ|sed 's/.*/(../)//1/'` aaa=`expr $aaa + 24` eval aaa=`echo $TZ|sed 's/..$/+$aaa/'` TZ=$aaa export TZ yy=`date +%Y` mm=`date +%m` dd=`date +%d` echo $yy$mm$dd
2、 遍历所有情况类似函数的方法
cat yesterday.sh
#!/bin/bash # Var Declare #!/bin/sh # ydate: A Bourne shell script that # prints yestarday's date # Output Form: Month Day Year # Set the current month day and year. month=`date +%m` day=`date +%d` year=`date +%Y` # Add 0 to month. This is a # trick to make month an unpadded integer. month=`expr $month + 0` # Subtract one from the current day. day=`expr $day - 1` # If the day is 0 then determine the last # day of the previous month. if [ $day -eq 0 ]; then # Find the preivous month. month=`expr $month - 1` # If the month is 0 then it is Dec 31 of # the previous year. if [ $month -eq 0 ]; then month=12 day=31 year=`expr $year - 1` # If the month is not zero we need to find # the last day of the month. else case $month in 1|3|5|7|8|10|12) day=31;; 4|6|9|11) day=30;; 2) if [ `expr $year % 4` -eq 0 ]; then if [ `expr $year % 400` -eq 0 ]; then day=29 fi else day=28 fi ;; esac fi fi echo yesterday:$year-${month}-${day}
说明:方法1较简洁明了,代码清晰;方法2是一般写程序的方法。
3、 [b]2中方法的进一步更新(支持20110101格式,即,年月固定用两位数)[/b]
cat yesterday.sh
#!/bin/bash # Var Declare #!/bin/sh # ydate: A Bourne shell script that # prints yestarday's date # Output Form: Month Day Year # Set the current month day and year. month=`date +%m` day=`date +%d` year=`date +%Y` # Add 0 to month. This is a # trick to make month an unpadded integer. month=`expr $month + 0` # Subtract one from the current day. day=`expr $day - 1` # If the day is 0 then determine the last # day of the previous month. if [ $day -eq 0 ]; then # Find the preivous month. month=`expr $month - 1` # If the month is 0 then it is Dec 31 of # the previous year. if [ $month -eq 0 ]; then month=12 day=31 year=`expr $year - 1` # If the month is not zero we need to find # the last day of the month. else case $month in 1|3|5|7|8|10|12) day=31;; 4|6|9|11) day=30;; 2) if [ `expr $year % 4` -eq 0 ]; then if [ `expr $year % 400` -eq 0 ]; then day=29 fi else day=28 fi ;; esac fi fi case $day in 1|2|3|4|5|6|7|8|9) day='0'$day esac case $month in 1|2|3|4|5|6|7|8|9) month='0'$month esac echo $year$month$day
说明:经过小小的改变,对实际应用提供了极大的方便。
相关文章推荐
- AIX 环境下取得昨天日期的 Shell
- aix shell 获取昨天的日期
- AIX环境下SHELL脚本设置成crontab调度和手动运行的某些不同
- linux中用shell获取昨天、明天或多天前的日期
- 用shell脚本,指定两个日期,计算相隔天数
- shell计算日期之间的天数
- shell与perl获取昨天、明天或多天前的日期
- 用shell脚本计算日期的小函数们
- 进行日期时间转换和计算的几个Shell小函数
- 用shell脚本计算日期的小函数们
- (转载)shell中用date命令获取昨天、明天或者多天前的日期
- Linux下shell计算两个日期之间的时间天数差,并获取出中间的日期
- shell计算二个日期中的天数
- shell 返回昨天和明天日期
- AIX 计算时间差shell
- Linux Bash Shell日期格式化和计算
- 【读书笔记】Linux Shell脚本攻略(一)打印 环境变量 计算 文件 数组 别名 日期 调试 函数
- 不同Unix环境下date计算日期的用法
- Mac Shell获取昨天日期