gpt4 book ai didi

linux - 在循环 bash 中等待

转载 作者:塔克拉玛干 更新时间:2023-11-02 23:51:42 24 4
gpt4 key购买 nike

我创建了一个循环来读取 .txt 并使用每一行作为输入来执行另一个 shell。

问题: 我需要循环并行执行 txt 中的前两行,等待它们完成,然后并行执行接下来的两行。

尝试解决方案:我想添加一个等待命令,只是不确定如何构造,所以它等待每两行,而不是循环的每次搅动。

我当前的循环:

cat input.txt | while read line; do
export step=${line//\"/}
export step=ExecuteModel_${step//,/_}
export pov=$line
$owsdirectory"/hpm_ws_client.sh" processCalcScriptOptions "$appName" "$pov" "$layers" "$stages" "" "$stages" "$stages" FALSE > "$appLogFolder""/"$step"_ProcessID.log"

/app/dev2/batch/hpcm/shellexe/rate_tool2/model_automation/check_process_status.sh "$appLogFolder" "$step" > "$appLogFolder""/""$step""_Monitor.log"

输入文本:

SEQ010,FY15
SEQ010,FY16
SEQ020,FY15
SEQ020,FY16
SEQ030,FY15
SEQ030,FY16
SEQ030,FY15
SEQ030,FY16
SEQ040,FY15
SEQ040,FY16
SEQ050,FY15
SEQ050,FY16

最佳答案

通常您会使用 semxargsparallel 来并行化一个循环,但所有这些工具都通过始终具有 2 (或 N) 作业并行运行,并在旧作业完成时启动新作业。

要改为运行成对的作业并等待它们完成后再考虑开始更多作业,您可以在后台运行它们并保留一个计数器以等待每 N 次迭代:

printf "%s\n" {1..10} | while IFS= read -r line
do
echo "Starting command"
sleep 2 &

if (( ++i % 2 == 0 ))
then
echo "Waiting..."
wait
fi
done

输出:

Starting command
Starting command
Waiting...
Starting command
Starting command
Waiting...

关于linux - 在循环 bash 中等待,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29853974/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com