繁体   English   中英

同时在多个文件上运行相同的Shell脚本

[英]Run the same shell script on multiple files at the same time

我有一个这样的shell脚本:

while read line
do
    a=`echo $line`

    while read line
    do
        <complex stuff>
    done < $a.txt
done < b.txt

其中b.txt包含文件名列表,该文件名作为第一个while循环的输入给出。

第二个while循环应将b.txt具有相同名称的文件作为输入并对其执行一些计算。

该脚本可以正常工作,但是这里的问题是第二个while循环过程必须并行处理b.txt中提到的所有文件,以节省完成完整任务所需的时间。

但是上面的脚本将从b.txt获取文件名,并完成耗时很长的任务。

可以修改此脚本,使其同时对b.txt中的所有文件执行第二个while循环吗?

您可能可以在后台运行每个内部循环(尽管在看不到身体的情况下无法确定地说。

while IFS= read -r line
do
    while read line
    do
        <complex stuff>
    done < "$line.txt" &
    #                  ^ A subshell is forked to run the loop, allowing
    #                    the outer loop to continue immediately.
done < b.txt
# Optional, but you may need to wait for all the background
# jobs to complete before proceeding
wait

暂无
暂无

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

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