Bash复杂管道依赖项 [英] Bash complex pipeline dependencies

查看:100
本文介绍了Bash复杂管道依赖项的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在单个Bash脚本中对构建并发管道进行建模.我知道我可以使用其他工具,但是出于理解Bash的目的,我正在这样做.

I'm trying to model a build concurrent pipeline in a single Bash script. I know I can use other tools, but at this point I'm doing it out of understanding Bash.

并行安排作业很容易,而在最后等待所有作业很容易.但我想通过在任务A.1&之后立即触发任务A.2来使其运行更快.任务X.要使自己更加努力,请执行任务A.1& A中的代码.任务A.2与&顺序的,所以如果我也能保持代码的顺序也很好.

Scheduling jobs parallel is easy, and waiting for them all at the end is easy. But I want to make it run faster by trigger Task A.2 immediately after Task A.1 & Task X. To make it even hard on myself, the code in Task A.1 & Task A.2 is related & sequential, so it would be nice if I could keep the code sequential as well.

#!/usr/bin/bash

{
    echo "Task X"
} &
DEPENDENCY=$!

{
    echo "Task A.1"
    wait "${DEPENDENCY}"
    echo "Task A.2"
} &

{
    echo "Task B.1"
    wait "${DEPENDENCY}"
    echo "Task B.2"
} &

wait

理想情况下,这是我想要的,但是它不起作用,因为子进程不能互相等待-这很有意义-但我希望我可以以跨平台的方式来完成这项工作.

This is ideally what I want, but it doesn't work because child processes can't wait for each other--which makes sense--but I'm hoping I can make this work in a cross platform way.

我实际上已经在工作,但是我无法保留* .1和* .2部分的代码

I actually have this working, but but I wasn't able to keep the code for part *.1 and *.2

如果这对OSX& amp;有效,那将是很好的. Linux.我希望Bash专家能够参与进来,并展示一种在Bash中表达这种表达的简洁方式.

Also it would be nice if this can work for OSX & Linux. I'm hoping a Bash expert can chime in and show a succinct way of expressing this in Bash.

推荐答案

一种使之适用于OSX& Linux将使用命名管道向批处理作业发出可以继续执行的信号.不幸的是,它需要拆分任务* .1"和任务* .2",但这是我目前能提出的最好的结果.

One way to make this work for OSX & Linux is to use namedpipes to signal to batch jobs that they can continue. Unfortunately it requires splitting up "Task *.1" & "Task *.2", but it's the best I can come up with for the moment.

#!/bin/bash

function cleanup {
  rm -f .task.a.1.done .task.b.1.done
}
trap cleanup EXIT
cleanup

mkfifo .task.a.1.done
mkfifo .task.b.1.done

{
    echo "Task A.1"
    echo -n "" > .task.a.1.done
} &

{
    echo "Task B.1"
    echo -n "" > .task.b.1.done
} &

{
    echo "Task X"
}

{
    cat < .task.a.1.done
    echo "Task A.2"
} &

{
    cat < .task.b.1.done
    echo "Task B.2"
} &

wait

这篇关于Bash复杂管道依赖项的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆