Bash 可以打印两个 iso8601 时间段之间所有日期的脚本
Bash script that can print all dates between two iso8601 time periods
我在 bash 中有以下正在进行的脚本。
#!/bin/bash
# usage:
# ./script.sh <iso8601_period>
period_ago() {
if [[ =~ P(([0-9]+)M)?([0-9]+)D ]]; then
local months=${BASH_REMATCH[2]:-0}
local days=${BASH_REMATCH[3]}
date -d "$months months $days days ago" "+%Y/%m/%d"
fi
}
period="$(period_ago )"
max=3
for (( i=0; i <= $max; ++i ))
do
temp=$i
if (( ${#temp} < 2 ))
then
temp="0$temp"
fi
echo $period/$temp
done
当你运行这个时,目前,这将打印:
2019/07/20/00
2019/07/20/01
2019/07/20/02
2019/07/20/03
我想弄清楚在 运行 脚本(传递两个参数)时我是否可以做这样的事情,它会给我两个 ISO8601 期间之间的日期,在本例中为 100 天前和 99 天前?
./script P100D P99D
2019/07/20/00
2019/07/20/01
2019/07/20/02
2019/07/20/03
2019/07/21/00
2019/07/21/01
2019/07/21/02
2019/07/21/03
基于 period_ago 函数和打印 'max' 行的循环已经开发:
period_ago {
...
}
start=$(period_ago )
end=$(period_ago )
max=3
d=$start
until [[ $d > $end ]] ; do
# Original Loop to max.
for (( i=0; i <= $max; ++i ))
do
temp=$i
if (( ${#temp} < 2 ))
then
temp="0$temp"
fi
echo $d/$temp
done
# Bump to next date
d=$(date -d "$d tomorrow" +'%Y-%m-%d')
done
更紧凑的版本
start=$(period_ago )
end=$(period_ago )
max=3
d=$start
until [[ $d > $end ]] ; do
# Original Loop to max.
for (( i=0; i <= $max; ++i )) ; do
printf "%s/%02d\n" $d $i
done
# Bump to next date
d=$(date -d "$d tomorrow" +'%Y/%m/%d')
done
我在 bash 中有以下正在进行的脚本。
#!/bin/bash
# usage:
# ./script.sh <iso8601_period>
period_ago() {
if [[ =~ P(([0-9]+)M)?([0-9]+)D ]]; then
local months=${BASH_REMATCH[2]:-0}
local days=${BASH_REMATCH[3]}
date -d "$months months $days days ago" "+%Y/%m/%d"
fi
}
period="$(period_ago )"
max=3
for (( i=0; i <= $max; ++i ))
do
temp=$i
if (( ${#temp} < 2 ))
then
temp="0$temp"
fi
echo $period/$temp
done
当你运行这个时,目前,这将打印:
2019/07/20/00
2019/07/20/01
2019/07/20/02
2019/07/20/03
我想弄清楚在 运行 脚本(传递两个参数)时我是否可以做这样的事情,它会给我两个 ISO8601 期间之间的日期,在本例中为 100 天前和 99 天前?
./script P100D P99D
2019/07/20/00
2019/07/20/01
2019/07/20/02
2019/07/20/03
2019/07/21/00
2019/07/21/01
2019/07/21/02
2019/07/21/03
基于 period_ago 函数和打印 'max' 行的循环已经开发:
period_ago {
...
}
start=$(period_ago )
end=$(period_ago )
max=3
d=$start
until [[ $d > $end ]] ; do
# Original Loop to max.
for (( i=0; i <= $max; ++i ))
do
temp=$i
if (( ${#temp} < 2 ))
then
temp="0$temp"
fi
echo $d/$temp
done
# Bump to next date
d=$(date -d "$d tomorrow" +'%Y-%m-%d')
done
更紧凑的版本
start=$(period_ago )
end=$(period_ago )
max=3
d=$start
until [[ $d > $end ]] ; do
# Original Loop to max.
for (( i=0; i <= $max; ++i )) ; do
printf "%s/%02d\n" $d $i
done
# Bump to next date
d=$(date -d "$d tomorrow" +'%Y/%m/%d')
done