繁体   English   中英

Bash:根据枚举数拆分循环以处理多个文件

[英]Bash: Split up loop based on number of enumerations to process several files

需要在bash中处理一堆文件,并根据枚举数拆分处理。 我想先处理1000个文件,然后再处理1000个文件,如果超出2000个文件的限制,则停止。 在python中,我将执行以下操作:

for e,any_file in enumerate("some_files"):
    if e<1000: # for the first 1000 files
        print(any_file) # or do anything you want
    else:
        if e<2000: # for the second 1000 files
            print(any_file)
        else:
            print("exceeded maximum limit!")

bash有什么类似的东西吗?

您可以使用一个计数器:

e=1  ## Counter
for f in /path/*; do
    if (( e < 1000 )); then
        echo "$f"
    else
        if (( e < 2000 )); then
            echo "$f"
        else
            echo "exceeded maximum limit!"
        fi
    fi
    (( ++e ))
done

或数组:

files=(0 /path/*); unset 'files[0]'
for e in "${!files[@]}"; do
    f=${files[e]}
    ...
done

您还可以创建一个枚举的输出数组一样find与过程替代和readarray

readarray -O 1 -t files < <(exec find ...)

有关所有内容,请参见Bash手册man bash )。 另请参阅helphelp <command>

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM