如何对单个文件和程序进行多次发球

How to tee multiple times to a single file and program

背景

我将以下命令作为 shell 脚本存储在 execution.sh

cat input_file | tee output_file | java program

我使用 ./execution.sh & 成功地从 input_file 中读取,将数据存储在单个 output_file 中,并将作为输入发送到 java 程序。

问题

我想将 input_file 的数据多次输出到 output_file 和 java 程序。

例如读取相同的 input_file 并行 5 次并将数据发送到单个 output_file 和单个 java program

编辑

尝试过的解决方案

execution.sh

{
 python2 readLines.py &
 python2 readLines.py &
 python2 readLines.py &
 python2 readLines.py &
 python2 readLines.py &
} | tee  output_file | java program 

readLines.py

with open('inputfile') as f:
    for line in f:
       print line

我目前正在使用这个,如果有人看到任何问题,例如竞争条件等,请在这里发表评论。

Reading same input_file say 5 times in parallel and send the data to a single output_file and single java program

让我们忽略 »parallel« 部分。写入只能顺序

( for i in {1..5}; do cat input_file; done ) | tee out_file | java program

或者简而言之

cat input_file{,,,,} | tee out_file | java program

这两个命令连续打印文件 5 次。

如果你真的想 并行编写 ,你可以启动 cat 命令作为后台作业:

( for i in {1..5}; do cat input_file & done ) | tee out_file | java program

此方法保证 output_file 包含来自 input_file 的所有字节恰好五次,但(当然)是交错的。很有可能不仅是行,而且字节最终也会交错。这是什么意思?

如果你有文件

abc
xyz

并并行打印两次,输出可能会变成

ababcc

xxyz
yz

如果这不打扰您,请记住,如果字节序列没有出现在该序列中,则 lose/change 它们的含义是有字节序列的,例如 windows new行 \r\n 或多字节 unicode 字符。