linux - 在两个脚本之间使用 I/O 重定向,无需等待第一个脚本完成

标签 linux bash shell pipeline io-redirection

我有两个脚本,比如说 long.sh 和 simple.sh:一个非常耗时,另一个非常简单。第一个脚本的输出应该用作第二个脚本的输入。

例如,“long.sh”可能是这样的:

#!/bin/sh
for line in `cat LONGIFLE.dat` do;
    # read line;
    # do some complicated processing (time consuming);
    echo $line
done;

最简单的是:

#!/bin/sh
while read a; do
    # simple processing;
    echo $a + "other stuff"
done;

我想将这两个脚本通过管道传输:

sh long.sh | sh simple.sh

使用管道,simple.sh 必须等待长脚本结束才能开始。

我想知道在 bash shell 中是否可以看到 simple.sh 的每一行的输出,这样我就可以在运行时看到此时正在处理哪一行。

我不希望将两个脚本合并在一起,也不希望在 long.sh 中调用 simple.sh。 非常感谢。

最佳答案

标准输出通常被缓冲。你想要行缓冲。尝试

stdbuf -oL sh long.sh | sh simple.sh

注意这个循环

for line in `cat LONGIFLE.dat`; do   # see where I put the semi-colon?

从文件中读取单词。如果你每行只有一个词,你就可以了。否则,要按行阅读,请使用 while IFS= read -r line; do ...; done < LONGFILE.dat

总是引用你的变量 ( echo "$line" ),除非你特别知道什么时候不引用。

关于linux - 在两个脚本之间使用 I/O 重定向,无需等待第一个脚本完成,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21859620/

相关文章:

linux - Bash:在模式之后将一个文件的内容插入到另一个文件中

macos - MAC OS X 10.10.2 中 top 命令中的 CMPRS 是什么?

android - 香气安装程序问题

c - 在不使用 XDr 的情况下在同一主机上使用 RPC

c - WEXITSTATUS(childStatus) 返回 0 但 waitpid 返回 -1

linux - Loggly 无法通过 Pi 上的 Winston 工作

bash - 回显到 stderr 和 tee 到日志文件?

linux - Bash 中的延迟监控

c - 如何检查2台服务器之间的状态?

linux - 如何测试文件中的某些字符