• linkedu视频
  • 平面设计
  • 电脑入门
  • 操作系统
  • 办公应用
  • 电脑硬件
  • 动画设计
  • 3D设计
  • 网页设计
  • CAD设计
  • 影音处理
  • 数据库
  • 程序设计
  • 认证考试
  • 信息管理
  • 信息安全
菜单
linkedu.com
  • 网页制作
  • 数据库
  • 程序设计
  • 操作系统
  • CMS教程
  • 游戏攻略
  • 脚本语言
  • 平面设计
  • 软件教程
  • 网络安全
  • 电脑知识
  • 服务器
  • 视频教程
  • vbs
  • DOS/BAT
  • hta/htc
  • python
  • perl
  • VBA
  • ColdFusion
  • ruby
  • PowerShell
  • Lua
  • Golang
  • linux shell
您的位置:首页 > 脚本语言 >linux shell > Shell脚本实现的阳历转农历代码分享

Shell脚本实现的阳历转农历代码分享

作者: 字体:[增加 减小] 来源:互联网

通过本文主要向大家介绍了shell脚本代码,shell脚本,shell脚本编程,shell脚本学习指南,shell脚本实例等相关知识,希望对您有所帮助,也希望大家支持linkedu.com www.linkedu.com

闲来无事,想在Linux下用shell写一个阳历转农历的脚本,断断续续大概一个星期终于搞定。现在拿出来与大家分享。

1、缘由

本脚本实现原理是查表法(因为公式有误差);基于农历新年为基准,对农历新年前后两个不同的农历进行计算。

写这个脚本之前是想在Linux 终端命令提示符中加入阳历及农历日期。在Ubuntu中有Lunar软件可以获取农历日期,但在Fedora或CentOS中并没有类似软件,所以就想自己来实现一个,但网上用其他语言写的一大把,如果再写没什么必要。所以就想用shell来写一个。

2、功能及使用

功能:将具体的阳历日期转换为农历日期。

时间范围:1901~2099,对应农历年时间为4598~4796

参数格式(无参数默认为当前系统日期):yyyymmdd

如2013年1月1日:

$./lunar.sh 20130101
4709-11-20
</div>

3、完整数据

完整数据下载链接:
http://xiazai.jb51.net/201408/tools/lunar-20131202.7z

包中文件:

lunar.sh 主脚本,具体实现

datebases 农历元数据

change.log 更改日志

readme 脚本说明及注意事项

主要脚本lunar.sh代码如下:

#!/bin/sh

DATE=$@
[ "$DATE" = "" ] && DATE=$(date +%Y%m%d)

databases_path=databases

date_year=$(echo $DATE |sed 's/^\(.\{4\}\).*/\1/')
date_month=$(echo $DATE |sed 's/.*\(..\)..$/\1/')
date_day=$(echo $DATE |sed 's/.*\(..\)$/\1/')
date_days=$(date -d $DATE +%j)

lunar_year=$(sed /$date_year/!d $databases_path |sed 's/^\(....\).*/\1/')

lunar_year_data=$(sed /$date_year/!d $databases_path |sed 's/.*\ \(.*\)/\1/')
lunar_year_data_bin=$(echo "ibase=16;obase=2;$lunar_year_data"|bc |sed -e :a -e 's/^.\{1,23\}$/0&/;ta')

new_year_month_bin=$(echo $lunar_year_data_bin |sed -e 's/^.\{17\}\(.\{2\}\).*/\1/')
new_year_month=$(echo "ibase=2;$new_year_month_bin"|bc |sed -e :a -e 's/^.\{1,1\}$/0&/;ta')

new_year_day_bin=$(echo $lunar_year_data_bin |sed -e 's/.*\(.\{5\}\)$/\1/')
new_year_day=$(echo "ibase=2;$new_year_day_bin"|bc |sed -e :a -e 's/^.\{1,1\}$/0&/;ta')

new_year_days=$(date -d $date_year$new_year_month$new_year_day +%j)
lunar_days=$(expr $date_days - $new_year_days + 1)

befor_or_after=0

if [ "$lunar_days" -le "0" ]; then
  befor_or_after=1
  date_year=$(($date_year - 1))

  lunar_year=$(sed /$date_year/!d $databases_path |sed 's/^\(....\).*/\1/')

  lunar_year_data=$(sed /$date_year/!d $databases_path |sed 's/.*\ \(.*\)/\1/')
  lunar_year_data_bin=$(echo "ibase=16;obase=2;$lunar_year_data"|bc |sed -e :a -e 's/^.\{1,23\}$/0&/;ta')
fi

lunar_leap_month_bin=$(echo $lunar_year_data_bin |sed -e 's/^\(.\{4\}\).*/\1/')
lunar_leap_month=$(echo "ibase=2;$lunar_leap_month_bin"|bc)

lunar_month_all_bin=$(echo $lunar_year_data_bin |sed -e 's/^.\{4\}\(.\{13\}\).*/\1/')
[ "$lunar_leap_month" = "0" ] && lunar_month_all_bin=$(echo $lunar_year_data_bin |sed -e 's/^.\{4\}\(.\{12\}\).*/\1/')
lunar_month_all=$(echo $lunar_month_all_bin |sed -e 's/0/29\ /g' |sed -e 's/1/30\ /g')

if [ "$befor_or_after" = "0" ];then
  lunar_month=1
  lunar_day=$lunar_days
  for i in $lunar_month_all
  do
   [ "$lunar_day" -gt "$i" ] && lunar_day=$(($lunar_day - $i)) && lunar_month=$(($lunar_month + 1))
   [ "$lunar_day" = "$i" ] && break
  done
else
  lunar_month=12
  lunar_day=$((-$lunar_days))
  lunar_month_all_bin=$(echo $lunar_month_all_bin |rev)
  lunar_month_all=$(echo $lunar_month_all_bin |sed -e 's/0/29\ /g' |sed -e 's/1/30\ /g')
  for i in $lunar_month_all
  do
   if [ "$lunar_day" -gt "$i" ]; then
     lunar_day=$(($lunar_day - $i))
     lunar_month=$(($lunar_month - 1))
   else
     lunar_day=$(($i - $lunar_day))
     break
   fi
  done
fi

if [ "$lunar_leap_month" = "0" ]; then
  echo $lunar_year-$lunar_month-$lunar_day
else
  if [ "$lunar_leap_month" -ge "$lunar_month" ]; then
   echo $lunar_year-$lunar_month-$lunar_day
  elif [ "$befor_or_after" = "0" ]; then
   if [ "$(($lunar_leap_month + 1))" = "$lunar_month" ];then
     lunar_month=$(($lunar_month - 1))
     echo $lunar_year-*$lunar_month-$lunar_day
   else
     lunar_month=$(($lunar_month - 1))
     echo $lunar_year-$lunar_month-$lunar_day
   fi
  else
   echo $lunar_year-$lunar_month-$lunar_day
  fi
fi

lunar.sh
</div>

4 修改历史

2013-12-02

发现bug:如果农历上个月是大月,本月为小月,则上个月的三十输出为本月的初一,原因是上个月剩下30天,这正好是上个月的三十,而本月是29天,29<30,下一次循环的时候又减本月的天数,使得上个月的三十成为本月的初一

bug修改:添加判断语句,如果农历剩余天数等于当月的天数则不再循环

</div>

您可能想查找下面的文章:

  • shell脚本nicenumber实现代码
  • Shell脚本实现的基于SVN的代码提交量统计工具
  • Shell脚本实现C语言代码行数统计
  • Shell脚本实现批量下载网络图片代码分享
  • Shell脚本实现的阳历转农历代码分享
  • Shell脚本实现批量下载网络图片代码分享
  • Shell脚本实现的阳历转农历代码分享
  • shell脚本中使用iconv实现批量文件转码的代码分享
  • 自动重启服务的shell脚本代码
  • shell脚本运行5秒后自动退出的代码

相关文章

  • linux shell之文件锁
  • Linux Shell脚本系列教程(七):脚本调试
  • 浅谈vim的四种模式及模式切换
  • 学习linux常用命令(推荐)
  • Shell脚本实现启动PHP内置FastCGI Server
  • bash 编程中循环语句用法
  • Shell脚本学习指南之文本处理工具
  • 一个简单的linux命令 mkdir
  • 分享一个实用的iptables脚本(各种过滤写法参考)
  • Shell脚本IF条件判断和判断条件总结

文章分类

  • vbs
  • DOS/BAT
  • hta/htc
  • python
  • perl
  • VBA
  • ColdFusion
  • ruby
  • PowerShell
  • Lua
  • Golang
  • linux shell

最近更新的内容

    • Shell命令批量修改图片文件名
    • Ubuntu配置NFS的具体流程(推荐)
    • shell脚本编程之数组
    • Linux Shell的一些使用小技巧收集
    • Shell脚本去重的几种方法实例
    • shell实现tr删除替换详解
    • shell字符串操作详解
    • Shell中判断字符串是否为数字的6种方法分享
    • Cygwin下安装vim后,vim中退格键无法正常使用的解决方法
    • linux下保留文件系统下剩余指定数目文件的shell脚本

关于我们 - 联系我们 - 免责声明 - 网站地图

©2020-2025 All Rights Reserved. linkedu.com 版权所有