日期处理脚本与Windows 10上Bash安装指南
1. GNU date的优势
GNU date是一个强大的日期处理工具,它能轻松处理复杂的日期计算。例如,判断某一年是否为闰年,只需使用以下代码:
if [ $( date 12/31/$year +%j ) -eq 366 ]如果一年的最后一天是该年的第366天,那么这一年就是闰年。此外,GNU date还能处理较早的日期,而标准Unix的date命令的起始时间是1970年1月1日00:00:00 UTC,无法处理早于该时间的日期。
2. 查找过去特定日期是星期几
以下脚本可以快速找出过去特定日期是星期几:
#!/bin/bash # dayinpast--Given a date, reports what day of the week it was if [ $# -ne 3 ] ; then echo "Usage: $(basename $0) mon day year" >&2 echo " with just numerical values (ex: 7 7 1776)" >&2 exit 1 fi date --version > /dev/null 2>&1 # Discard error, if any. baddate="$?" # Just look at return code. if [