shell脚本产生指定时间段内的每月开始时间和结束时间

本文介绍了一个Shell脚本,该脚本可以根据输入的年份、起始月份和结束月份,生成每个月的第一天和最后一天的具体日期。通过简单的循环和日期操作命令,实现了批量获取指定时间段内各月日期的功能。

shell脚本产生指定时间段内的每月开始时间和结束时间:

year_month_first_last_date.sh

#!/usr/bin/env bash
#输入参数  1=年份  2=开始月份  3=结束月份
for i in `seq -w $2 $3`; do
#参数1为指定的年份
year=$1
echo $year
monthday=${year}${i}01
echo $monthday

#产生每月第一天
begin_date=`date --date "$monthday" +"%Y-%m-01"`
echo $begin_date

#产生每月最后一天
year=`date -d "$monthday" +"%Y"`
month=`date -d "$monthday" +"%m"`
last_day=`cal $month $year | xargs | awk '{print $NF}'`

end_date=$year-$month-$last_day
echo $end_date

echo $i

#sh cpro_wap_tu_detail_result_year.sh $begin_date $end_date $i $year

done

测试:

 sh year_month_first_last_date.sh 2016 01 05
2016
2016101
206-01-01
206-01-31
1
2016
2016201
206-02-01
206-02-28
2
2016
2016301
206-03-01
206-03-31
3
2016
2016401
206-04-01
206-04-30
4
2016
2016501
206-05-01
206-05-31
5



### 统计某一时间段内提交的代码行数 要统计在 Git 中某一时间段内的代码提交行数,可以结合 `git log` `--stat`、`--numstat` ://参数来获取详细的增删行信息,并通过管道符 `|` 将输出传递给 `awk` 进行统计处理。 #### 使用 `--stat` `awk` 统计总行数 ```bash git log --since="2023-10-01" --until="2023-10-31" --stat | grep -E " files? changed" | awk '{insertions+=$4; deletions+=$6} END {print "Total insertions: " insertions "\nTotal deletions: " deletions}' ``` 此命令将列出 2023 年 10 月 1 日至 10 月 31 日之间所有提交的更改统计信息,并计算总的新增行数(`insertions`)删除行数(`deletions`)[^2]。 #### 使用 `--numstat` 更精确地统计行数 ```bash git log --since="2023-10-01" --until="2023-10-31" --numstat | awk '/^[0-9]/ {insertions += $1; deletions += $2} END {print "Total insertions: " insertions "\nTotal deletions: " deletions}' ``` 该命令会显示更详细的每文件增删行数据,并通过 `awk` 汇总所有提交的插入删除行数。相比 `--stat`,`--numstat` 提供了更准确的统计方式。 #### 按作者统计某时间段内的提交行数 如果需要按作者分别统计提交的代码行数,可使用以下命令: ```bash git log --since="2023-10-01" --until="2023-10-31" --author="John" --numstat | awk '/^[0-9]/ {insertions += $1; deletions += $2} END {print "Insertions: " insertions ", Deletions: " deletions}' ``` 该命令仅筛选指定作者在特定时间范围内的提交,并计算其代码增删行数。 #### 结合脚本进行多作者统计 如果需统计多个开发者的提交情况,可以编写简单的 shell 脚本来遍历所有作者并输出结果: ```bash authors=("John" "Alice" "Bob") for author in "${authors[@]}" do echo "Stats for $author:" git log --since="2023-10-01" --until="2023-10-31" --author="$author" --numstat | awk '/^[0-9]/ {insertions += $1; deletions += $2} END {print "Insertions: " insertions ", Deletions: " deletions}' done ```
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值