Shell: days and dates
2019独角兽企业重金招聘Python工程师标准>>>
Code
#!/bin/bash
# given month, day and weekday, get the most recent year that match itusage() {echo "$(basename $0) weekday month day"echo "(example: $(basename $0) wed aug 3 )"exit 1
}# check for arguments
if [ $# -ne 3 ]; thenusage
fi
weekday=$1
month=$2
day=$3# parse arguments, translate them to lowcase, truncate them to 3 chars
weekday=$(echo $weekday | tr '[:upper:]' '[:lower:]' | cut -c1-3)
month=$(echo $month | tr '[:upper:]' '[:lower:]' | cut -c1-3)# get the current date
eval $(date "+thismonth=%m; thisday=%d; thisyear=%Y")# get month number and weekday number
monthnum=$(echo $month | sed -e \'s/jan/1/;s/feb/2/;s/mar/3/;s/apr/4/;s/may/5/;s/jun/6/;s/jul/7/;s/aug/8/;s/sep/9/;s/oct/10/;s/nov/11/;s/dec/12/')
weekdaynum=$(echo $weekday | sed -e \'s/mon/1/;s/tue/2/;s/wed/3/;s/thu/4/;s/fri/5/;s/sat/6/;s/sun/7/;')
monthname=$(echo $month | sed -e \'s/10/oct/;s/11/nov/;s/12/dec/;s/1/jan/;s/2/feb/;s/3/mar/;s/4/apr/;s/5/may/;s/6/jun/;s/7/jul/;s/8/aug/;s/9/sep/')
# get the most recent year to start with
if [ $monthnum -gt $thismonth ]; thenmostrecent=$(($thisyear - 1))
elif [ $monthnum -eq $thismonth -a $day -gt $thisday ]; thenmostrecent=$(($thisyear - 1))
elsemostrecent=$thisyear
fiecho $monthname $day $mostrecent
# iterate over the years, looking for a match
weekdaynum_of_that_year=$(date -d "$monthname $day $mostrecent" +%u)
while [ $weekdaynum_of_that_year -ne $weekdaynum ]; domostrecent=$(($mostrecent-1))weekdaynum_of_that_year=$(date -d "$monthname $day $mostrecent" +%u)
done# output result
echo "Most recent year for weekday=$weekday, month=$month, day=$day is $mostrecent"
Code
(another way to implement it -- parse the output result of cal with sed, tr and cut)
#!/bin/bash
# given month, day and weekday, get the most recent year that match it
# set -x
usage() {echo "$(basename $0) weekday month day"echo "(example: $(basename $0) wed aug 3 )"exit 1
}# check for arguments
if [ $# -ne 3 ]; thenusage
fi
weekday=$1
month=$2
day=$3# parse arguments, translate them to lowcase, truncate them to 3 chars
weekday=$(echo $weekday | tr '[:upper:]' '[:lower:]' | cut -c1-3)
month=$(echo $month | tr '[:upper:]' '[:lower:]' | cut -c1-3)# get the current date
eval $(date "+thismonth=%m; thisday=%d; thisyear=%Y")# get month number and weekday number
monthnum=$(echo $month | sed -e \'s/jan/1/;s/feb/2/;s/mar/3/;s/apr/4/;s/may/5/;s/jun/6/;s/jul/7/;s/aug/8/;s/sep/9/;s/oct/10/;s/nov/11/;s/dec/12/')
weekdaynum=$(echo $weekday | sed -e \'s/mon/1/;s/tue/2/;s/wed/3/;s/thu/4/;s/fri/5/;s/sat/6/;s/sun/7/;')
monthname=$(echo $month | sed -e \'s/10/oct/;s/11/nov/;s/12/dec/;s/1/jan/;s/2/feb/;s/3/mar/;s/4/apr/;s/5/may/;s/6/jun/;s/7/jul/;s/8/aug/;s/9/sep/')
# get the most recent year to start with
if [ $monthnum -gt $thismonth ]; thenmostrecent=$(($thisyear - 1))
elif [ $monthnum -eq $thismonth -a $day -gt $thisday ]; thenmostrecent=$(($thisyear - 1))
elsemostrecent=$thisyear
fi# parse the output of cal and get the answer
while true; docmd="cal -h ${monthnum} ${mostrecent} | grep \"\b${day}\b\" | sed 's/[ ][ ][ ]\|[ ][ ]\|[ ]/|/g; s/^|//; s/|/\n/g' | cat -n | grep \"\b${day}\b$\" | tr -s [:space:] ' '| cut -d' ' -f2"weekdaynum_of_that_year=$(eval $cmd)weekdaynum_of_that_year=$(($weekdaynum_of_that_year - 1))[ $weekdaynum_of_that_year -eq 0 ] && weekdaynum_of_that_year=7if [ $weekdaynum_of_that_year -eq $weekdaynum ]; then break; filet mostrecent=$mostrecent-1# echo -n "Debug: press [enter] to continue ..."# read ign
done# output result
echo "Most recent year for weekday=$weekday, month=$month, day=$day is $mostrecent"
-h option for cal is necessary for dealing with 'today' correctly
Code
转载于:https://my.oschina.net/u/158589/blog/85945
Shell: days and dates相关推荐
- 运维中的日志切割操作梳理(Logrotate/python/shell脚本实现)
对于Linux系统安全来说,日志文件是极其重要的工具.不知为何,我发现很多运维同学的服务器上都运行着一些诸如每天切分Nginx日志之类的CRON脚本,大家似乎遗忘了Logrotate,争相发明自己的轮 ...
- 109个shell实用脚本
109个shell实用脚本 1.Dos 攻击防范(自动屏蔽攻击 IP) #!/bin/bash DATE=$(date +%d/%b/%Y:%H:%M) LOG_FILE=/usr/local/ngi ...
- shell常用命令高级部分
日志统计常用手法: awk '{print $1}' |sort -n |uniq -c |sort -nr|head -10 找第一列中,排序,再统计每种多少,排序反序,再取前10名.ifconfi ...
- jar包部署shell脚本编写,在服务器上部署jar包,在Linux服务器上部署服务,设置编码格式,设置内存管理
准备步骤: 1.安装java环境,知道java安装目录 2.将jar包拖放或发送至服务器中(目录自定义) 一.编写shell脚本,将以下代码放在shell脚本中,将shell脚本放在jar包同级目录下 ...
- Linux shell 学习笔记(16)— shell 入门总结
1. Shell 变量 Shell 变量包括自定义变量和环境变量: 自定义变量:脚本中自己命名定义的变量,通常为局部变量,其他 Shell 程序不能访问到: 环境变量,操作系统已定义的变量,如 PAT ...
- 浅显易懂 Makefile 入门 (08)— 默认 shell (/bin/sh)、命令回显、make参数(-n 只显示命令但不执行,-s 禁止所有回显)、单行命令、多行命令、并发执行
1. shell 相关 1.1 默认 shell Makefile 所使用的命令是由 shell 命令行组成,他们是一条一条执行的. 多个命令之间要使用分号隔开,Makefile 中的任何命令都要以 ...
- Linux shell 学习笔记(12)— linux 信号、后台运行脚本、作业控制、定时运行任务
1. 处理信号 1.1 Linux 信号 常见的 Linux 信号如下表所示: 信号 值 描述 1 SIGHUP 挂起进程 2 SIGINT 终止进程 3 SIGQUIT 停止进程 9 SIGKILL ...
- Linux shell 学习笔记(11)— 理解输入和输出(标准输入、输出、错误以及临时重定向和永久重定向)
1. 理解输入和输出 1.1 标准文件描述符 Linux 系统将每个对象当作文件处理.这包括输入和输出进程.Linux 用文件描述符(file descriptor)来标识每个文件对象.文件描述符是一 ...
- Linux shell 学习笔记(10)— 处理用户输入(命令行读取参数、读取用户输入、超时处理)
1. 命令行参数 向 shell 脚本传递数据的最基本方法是使用命令行参数.命令行参数允许在运行脚本时向命令行添加数据. $ ./addem 10 30 本例向脚本 addem 传递了两个命令行参数( ...
最新文章
- GROMACS运行参数整理(二)
- MIMO雷达比幅单脉冲测角精度分析(系统工程与电子技术)
- 经常关注的、极具参考价值的网站收集(无限畅想版)
- python 共享内存变量_浅谈python多进程共享变量Value的使用tips
- 关于JavaScript处理字符串的常见操作
- python报错:Exception Value:can only concatenate str (not bytes) to str
- VOS防止盗打及后门的安全策略
- 基于SSM的网上购物商城管理系统
- 游戏安全02:手游外挂简单分类和实现原理介绍
- ddr3ddr4 lpddr4速率_LPDDR3一定弱?实测对比单双通道DDR4
- 小程序apkg还原_狐妖小红娘手游背后,有这样一群“天真”的人
- 莫名其妙CSDN不让我发帖子了。看来10年的老水手我该离开CSDN了。
- html标签对应的英文原文,[转载]片尾字幕中英文对照
- 【5号课堂】scratch制作电子生日贺卡
- Atmel和SIGFOX在远程物联网连接领域开展合作
- hw1 COVID-19 Cases prediction
- Android应用优化之流畅度优化实操
- 【技术分享】什么是K折交叉验证?
- $.ajax 的使用方法及参数说明
- hacking 麦步手表之(1)精简模拟界面
热门文章
- 恶意软件利用合法的代码签名证书横行Windows 系统
- 谷歌开源容器镜像的签名和验证工具 Cosign
- 神秘黑客攻陷密码管理器 Passwordstate 部署恶意软件,发动软件供应链攻击
- 我俩也组了个队,找到一个苹果RCE 0day,获 $5 万奖金
- Intel 警告注意 CSME 引擎中的严重漏洞,发布产品停产通知
- AMD ATI Radeon 显卡被曝多个漏洞
- 20164317《网络对抗技术》Exp3 免杀原理与实践
- 《从0到1学习Flink》—— Flink parallelism 和 Slot 介绍
- “中国工程设计大师”俞加康:为地铁耕耘“时不我待,只争朝夕”
- 一个广为人知但鲜有人用的技巧:对象池