多链管道

发布于 2024-11-27 04:03:05 字数 487 浏览 1 评论 0原文

绝望,我正在寻找一个月的多链管道的参考/源代码,这意味着我可以运行一些东西:

     cat /tmp/test.log  | wc -l --> stdout
                        | grep test1 --> stdout
                        | grep test2 | grep test3 |
                                                  | grep test4 --> stdout
                                                  | grep test5 --> stdout

请不要将我发送到bash/tee/$...命令,而 tee 处理文件,而 bash 太复杂而难以理解...

请注意,顺序并不重要,完成的第一个链也将被处理。

I am desperate, I am searching for a month for reference/source code for multi chains of pipes, meaning that I can run something:

     cat /tmp/test.log  | wc -l --> stdout
                        | grep test1 --> stdout
                        | grep test2 | grep test3 |
                                                  | grep test4 --> stdout
                                                  | grep test5 --> stdout

Please don't send me to bash/tee/$... commands while tee works with files, and bash is too complicated to understand...

Note that the order does not care, the first chain that was finished will be handled as well.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

今天小雨转甜 2024-12-04 04:03:05

你或许应该将你正在做的事情分解成多个较小的任务,而不是试图一击粉碎所有事情。这是一个 BASH 脚本,它可以完成您想要的相同操作,但更容易理解。

注意:我没有对此进行测试,因此可能存在问题。

#!/bin/bash

FILENAME='/tmp/test.log'
TMPNAME='mktemp -t' || exit

wc -l $FILENAME
grep 'test1' $FILENAME
awk '/test2/ && /test3/ {print}' $FILENAME > $TMPNAME
grep 'test4' $TMPNAME
grep 'test5' $TMPNAME

rm $TMPNAME

You should probably break what you are doing up into multiple smaller tasks rather than trying to smash everything in one blow. Here is a BASH script that will do the same thing you want, but be much easier to comprehend.

Note: I did not test this, so there may be issues.

#!/bin/bash

FILENAME='/tmp/test.log'
TMPNAME='mktemp -t' || exit

wc -l $FILENAME
grep 'test1' $FILENAME
awk '/test2/ && /test3/ {print}' $FILENAME > $TMPNAME
grep 'test4' $TMPNAME
grep 'test5' $TMPNAME

rm $TMPNAME
橪书 2024-12-04 04:03:05

实际上我必须为学校项目开发类似的东西。这个想法是概括管道的概念来实现这样的事情:(

 process1  --> process2
           --> process3 --> stdout
           --> process4 --> process5 --> stdout
                        --> process6 --> stdout

循环也是可能的)

为了做到这一点,我的程序做了以下事情:

  • 解析包含管道图(进程和管道)的文件
  • 使用 fork()为了为每个子进程创建子进程
  • ,我关闭传入管道的写入端,关闭传出管道的读取端,然后调用 execl()。其余的由子进程处理(注意死锁!),具体取决于您想要实现的目标。

就我而言,由于调用的进程也是由我开发的,因此我可以将管道文件描述符作为参数传递给子进程(--pipe-in​​ .... --pipe-out ....),但是由于您正在使用基于 stdin/stdout 的现有程序,因此您可以使用 dup2() 来“复制”标准 I/O 管道。

我不知道你是否打算这样做,但这样做是可行的。

I actually had to develop something similar for a school project. The idea was to generalize the notion of pipes to achieve something like this :

 process1  --> process2
           --> process3 --> stdout
           --> process4 --> process5 --> stdout
                        --> process6 --> stdout

(loops are also possible)

In order to do this, my program did the following things:

  • parse a file containing the pipe graph (processes and pipes)
  • use fork() to create child processes
  • for every child process, I close the writing end of the incoming pipes, I close the reading end of the outgoing pipes and I call execl(). The rest is handled by the child processes (beware of deadlocks!), depending on what you are trying to achieve.

In my case, since the processes called were also developed by me, I could just pass the pipes file descriptors as arguments to the child processes (--pipe-in .... --pipe-out ....), but since you are using existing programs based on stdin/stdout, you could use dup2() to "copy" the standard I/O pipes.

I do not know if this is how you plan on doing this, but it is doable that way.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文