等待在外壳脚本中不起作用



我正在运行一个 for 循环,其中使用 &.最后,我希望所有命令都返回值。

这是我尝试的代码

for((i=0 ;i<3;i++)) {
    // curl command which returns a value   & 
    } 
    wait

下一段代码

我想获取所有三个返回值,然后继续。.但 wait 命令不会等待后台进程完成并运行代码的下一部分。 我需要返回的值才能继续。.

Shell 内置的文档可通过 help BUILTIN_NAME 访问。 help wait产量:

wait: wait [-n] [id ...]
    Wait for job completion and return exit status.
    Waits for each process identified by an ID, which may be a process ID or a
    job specification, and reports its termination status.  If ID is not
    given, waits for all currently active child processes, and the return
    status is zero.  If ID is a a job specification, waits for all processes
    in that job's pipeline.
    If the -n option is supplied, waits for the next job to terminate and
    returns its exit status.
    Exit Status:
    Returns the status of the last ID; fails if ID is invalid or an invalid
    option is given.

这意味着要获取返回状态,您需要保存 PID,然后使用 wait $THE_PID 等待每个 PID。

例:

sl() { sleep $1; echo $1; return $(($1+42));  }
pids=(); for((i=0;i<3;i++)); do sl $i & pids+=($!); done; 
for pid in ${pids[@]}; do wait $pid; echo ret=$?; done

示例输出:

0
ret=42
1
ret=43
2
ret=44

编辑

使用 curl,不要忘记传递 -f ( --fail (,以确保如果 HTTP 请求失败,该过程将失败:

卷曲示例:

#!/bin/bash
URIs=(
    https://pastebin.com/raw/w36QWU3D 
    https://pastebin.com/raw/NONEXISTENT
    https://pastebin.com/raw/M9znaBB2
     )
pids=(); for((i=0;i<3;i++)); do
    curl -fL "${URIs[$i]}" &>/dev/null &
    pids+=($!)
done
for pid in "${pids[@]}"; do
    wait $pid
    echo ret=$?
done

卷曲示例输出:

ret=0
ret=22
ret=0

GNU Parallel 是并行执行高延迟操作(如curl(的好方法。

parallel curl --head {} ::: www.google.com www.hp.com www.ibm.com

或者,筛选结果:

parallel curl --head -s {} ::: www.google.com www.hp.com www.ibm.com | grep '^HTTP'
HTTP/1.1 302 Found
HTTP/1.1 301 Moved Permanently
HTTP/1.1 301 Moved Permanently

这是另一个例子:

parallel -k 'echo -n Starting {} ...; sleep 5; echo done.'  ::: 1 2 3 4
Starting 1 ...done.
Starting 2 ...done.
Starting 3 ...done.
Starting 4 ...done.

相关内容

  • 没有找到相关文章

最新更新