bash循环在两个给定的日期之间

前端之家收集整理的这篇文章主要介绍了bash循环在两个给定的日期之间前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我正在尝试创建一个脚本,循环使用以下格式编写文件名的文件:yyyymmdd.hh.filename.

脚本被调用

./loopscript.sh 20091026.00 23
./loopscript.sh 20091026.11 15
./loopscript.sh 20091026.09 20091027.17

需要脚本来检查这两个给定日期/小时之间的每小时.

例如

cat 20091026.00.filename |more
cat 20091026.01.filename |more
...
cat 20091026.23.filename |more
cat 20091027.01.filename |more
cat 20091027.02.filename |more
...

等等.

有什么想法呢?标准的0 – x循环没有任何困难.或简单的循环.只是不知道如何去上述.

要在两个给定的日期/时间之间处理每个文件,您可以使用以下内容
#!/usr/bin/bash
#set -x

usage() {
    echo 'Usage: loopscript.sh <from> <to>'
    echo '       <from> MUST be yyyymmdd.hh or empty,meaning 00000000.00'
    echo '       <to> can be shorter and is affected by <from>'
    echo '         e.g.,20091026.00       27.01 becomes'
    echo '               20091026.00 20091027.01'
    echo '         If empty,it is set to 99999999.99'
    echo 'Arguments were:'
    echo "   '${from}'"
    echo "   '${to}'"
}

# Check parameters.

from="00000000.00"
to="99999999.99"
if [[ ! -z "$1" ]] ; then
    from=$1
fi
if [[ ! -z "$2" ]] ; then
    to=$2
fi
## Insert this to default to rest-of-day when first argument
##    but no second argument. Basically just sets second
##    argument to 23 so it will be transformed to end-of-day.
#if [[ ! -z "$1"]] ; then
#    if [[ -z "$2"]] ; then
#        to=23
#    fi
#fi

if [[ ${#from} -ne 11 || ${#to} -gt 11 ]] ; then
    usage
    exit 1
fi

# Sneaky code to modify a short "to" based on the start of "from".
# ${#from} is the length of ${from}.
# $((${#from}-${#to})) is the length difference between ${from} and ${to}
# ${from:0:$((${#from}-${#to}))} is the start of ${from} long enough
#   to make ${to} the same length.
# ${from:0:$((${#from}-${#to}))}${to} is that with ${to} appended.
# Voila! Easy,no?

if [[ ${#to} -lt ${#from} ]] ; then
    to=${from:0:$((${#from}-${#to}))}${to}
fi

# Process all files,checking that they're inside the range.

echo "From ${from} to ${to}"
for file in [0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9].[0-9][0-9].* ; do
    if [[ ! ( ${file:0:11} < ${from} || ${file:0:11} > ${to} ) ]] ; then
        echo "   ${file}"
    fi
done

当您创建文件20091026.00.${RANDOM}到20091028.23.${RANDOM}包含,这是几个示例运行:

pax> ./loopscript.sh 20091026.07 9
From 20091026.07 to 20091026.09
   20091026.07.21772
   20091026.08.31390
   20091026.09.9214
pax> ./loopscript.sh 20091027.21 28.02
From 20091027.21 to 20091028.02
   20091027.21.22582
   20091027.22.30063
   20091027.23.29437
   20091028.00.14744
   20091028.01.6827
   20091028.02.10366
pax> ./loopscript.sh 00000000.00 99999999.99 # or just leave off the parameters.
   20091026.00.25772
   20091026.01.25964
   20091026.02.21132
   20091026.03.3116
   20091026.04.6271
   20091026.05.14870
   20091026.06.28826
   : : :
   20091028.17.20089
   20091028.18.13816
   20091028.19.7650
   20091028.20.20927
   20091028.21.13248
   20091028.22.9125
   20091028.23.7870

你可以看到,第一个参数必须是正确的格式yyyymmdd.hh.第二个参数可以缩短,因为它继承了第一个参数的开始以使其成为正确的长度.

这只会尝试处理存在的文件(从ls)和正确的格式,而不是每个范围内的每个日期/时间.如果您有稀疏文件(包括在范围的开始和结束),这将更有效,因为它不需要检查文件是否存在.

顺便说一下,这是创建测试文件的命令,如果你有兴趣:

pax> for dt in 20091026 20091027 20091028 ; do
         for tm in 00 01 02 ... you get the idea ... 21 22 23 ; do
             touch $dt.$tm.$RANDOM
         done
     done

请不要逐字输入,然后抱怨它创建的文件如下:

20091026.you.12345
20091028.idea.77

我只修剪了一行,所以它符合代码宽度.

猜你在找的Bash相关文章