bash shell 日期解析,从特定日期开始并循环遍历每月的每一天
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2655883/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
bash shell date parsing, start with specific date and loop through each day in month
提问by Joe Stein
I need to create a bash shell script starting with a day and then loop through each subsequent day formatting that output as %Y_%m_d
我需要创建一个从一天开始的 bash shell 脚本,然后循环遍历随后每一天的格式,输出为 %Y_%m_d
I figure I can submit a start day and then another param for the number of days.
我想我可以提交一个开始日期,然后提交另一个天数参数。
My issue/question is how to set a DATE (that is not now) and then add a day.
我的问题是如何设置日期(不是现在)然后添加一天。
so my input would be 2010_04_01 6
所以我的输入是 2010_04_01 6
my output would be
我的输出是
2010_04_01
2010_04_02
2010_04_03
2010_04_04
2010_04_05
2010_04_06
回答by eRadical
[radical@home ~]$ cat a.sh
#!/bin/bash
START=`echo | tr -d _`;
for (( c=0; c<; c++ ))
do
echo -n "`date --date="$START +$c day" +%Y_%m_%d` ";
done
Now if you call this script with your params it will return what you wanted:
现在,如果您使用参数调用此脚本,它将返回您想要的内容:
[radical@home ~]$ ./a.sh 2010_04_01 6
2010_04_01 2010_04_02 2010_04_03 2010_04_04 2010_04_05 2010_04_06
回答by anonymous
Very basic bash script should be able to do this:
非常基本的 bash 脚本应该能够做到这一点:
#!/bin/bash
start_date=20100501
num_days=5
for i in `seq 1 $num_days`
do
date=`date +%Y/%m/%d -d "${start_date}-${i} days"`
echo $date # Use this however you want!
done
Output:
2010/04/30
2010/04/29
2010/04/28
2010/04/27
2010/04/26
产出:
2010/04/30
2010/04/29
2010/04/28
2010/04/27
2010/04/26
回答by Tom
Note: NONE of the solutions here will work with OS X. You would need, for example, something like this:
注意:这里的任何解决方案都不适用于 OS X。例如,您需要这样的东西:
date -v-1d +%Y%m%d
日期 -v-1d +%Y%m%d
That would print out yesterday for you. Or with underscores of course:
昨天会为你打印出来。或者当然有下划线:
date -v-1d +%Y_%m_%d
日期 -v-1d +%Y_%m_%d
So taking that into account, you should be able to adjust some of the loops in these examples with this command instead. -v option will easily allow you to add or subtract days, minutes, seconds, years, months, etc. -v+24d would add 24 days. and so on.
因此,考虑到这一点,您应该能够使用此命令来调整这些示例中的某些循环。-v 选项将允许您轻松添加或减去天、分、秒、年、月等。 -v+24d 将添加 24 天。等等。
回答by Paused until further notice.
#!/bin/bash
inputdate="${1//_/-}" # change underscores into dashes
for ((i=0; i<; i++))
do
date -d "$inputdate + $i day" "+%Y_%m_%d"
done
回答by Gianluca Casati
You can also use cal, for example
您也可以使用cal,例如
YYYY=2014; MM=02; for d in $(cal $MM $YYYY | grep "^ *[0-9]"); do DD=$(printf "%02d" $d); echo $YYYY$MM$DD; done
(originally posted here on my commandlinefu account)
(最初发布在我的 commandlinefu 帐户上)
回答by anonymous
Very basic bash script should be able to do this.
非常基本的 bash 脚本应该能够做到这一点。
Script:
#!/bin/bash
start_date=20100501
num_days=5
for i in seq 1 $num_days
do
date=date +%Y/%m/%d -d "${start_date}-${i} days"
echo $date # Use this however you want!
done
脚本:
#!/bin/bash
start_date=20100501
num_days=5
for i in seq 1 $num_days
do
date= date +%Y/%m/%d -d "${start_date}-${i} days"
echo $date # 随心所欲地使用它!
完毕
Output:
2010/04/30
2010/04/29
2010/04/28
2010/04/27
2010/04/26
产出:
2010/04/30
2010/04/29
2010/04/28
2010/04/27
2010/04/26
回答by Jürgen H?tzel
You can pass a date via command line option -dto GNU date handling multiple input formats:
您可以通过命令行选项-d将日期传递给处理多种输入格式的 GNU 日期:
http://www.gnu.org/software/coreutils/manual/coreutils.html#Date-input-formats
http://www.gnu.org/software/coreutils/manual/coreutils.html#Date-input-formats
Pass starting date as command line argument or use current date:
将开始日期作为命令行参数传递或使用当前日期:
underscore_date=${1:-$(date +%y_%m_%d)}
date=${underscore_date//_/-}
for days in $(seq 0 6);do
date -d "$date + $days days" +%Y_%m_%d;
done
回答by ghostdog74
you can use gawk
你可以使用gawk
#!/bin/bash
DATE=
num=
awk -vd="$DATE" -vn="$num" 'BEGIN{
m=split(d,D,"_")
t=mktime(D[1]" "D[2]" "D[3]" 00 00 00")
print d
for(i=1;i<=n;i++){
t+=86400
print strftime("%Y_%m_%d",t)
}
}'
output
输出
$ ./shell.sh 2010_04_01 6
2010_04_01
2010_04_02
2010_04_03
2010_04_04
2010_04_05
2010_04_06
2010_04_07