简体   繁体   English

将输出从文件重定向到stdout

[英]Redirect output from file to stdout

I have a program that can output its results only to files, with -o option. 我有一个程序只能将结果输出到文件,使用-o选项。 This time I need to output it to console, ie stdout . 这次我需要将它输出到控制台,即stdout Here is my first try: 这是我的第一次尝试:

myprog -o /dev/stdout input_file

But it says: 但它说:

/dev/ not writable / dev / not writable

I've found this question that's similar to mine, but /dev/stdout is obviously not going to work without some additional magic. 我发现这个问题与我的相似,但是如果没有一些额外的魔法, /dev/stdout显然不会起作用。

Q: How to redirect output from file to stdout ? 问:如何将输出从文件重定向到stdout

PS Conventional methods without any specialized software are preferable. PS没有任何专用软件的传统方法是优选的。

Many tools interpret a - as stdin/stdout depending on the context of its usage. 许多工具根据其使用的上下文将-解释为stdin / stdout。 Though, this is not part of the shell and therefore depends on the program used. 虽然,这不是shell的一部分,因此取决于所使用的程序。

In your case the following could solve your problem: 在您的情况下,以下可以解决您的问题:

myprog -o - input_file

If the program can only write to a file, then you could use a named pipe: 如果程序只能写入文件,那么您可以使用命名管道:

pipename=/tmp/mypipe.$$
mkfifo "$pipename"

./myprog -o "$pipename" &

while read line
do
    echo "output from myprog: $line"
done < "$pipename"

rm "$pipename"

First we create the pipe, we put it into /tmp to keep it out of the way of backup programs. 首先我们创建管道,我们将它放入/tmp以使其不受备份程序的影响。 The $$ is our PID, and makes the name unique at runtime. $$是我们的PID,并且在运行时使名称唯一。

We run the program in background, and it should block trying to write to the pipe. 我们在后台运行程序,它应该阻止尝试写入管道。 Some programs use a technique called "memory mapping" in which case this will fail, because a pipe cannot be memory mapped (a good program would check for this). 有些程序使用称为“内存映射”的技术,在这种情况下,这将失败,因为管道无法进行内存映射(一个好的程序会检查这一点)。

Then we read the pipe in the script as we would any other file. 然后我们像在任何其他文件中一样读取脚本中的管道。

Finally we delete the pipe. 最后我们删除了管道。

You can cat the contents of the file written by myprog . 您可以cat在文件的内容写的myprog

myprog -o tmpfile input_file && cat tmpfile

This would have the described effect -- allowing you to pipe the output of myprog to some subsequent command -- although it is a different approach than you had envisioned. 这将具有所描述的效果 - 允许您将myprog的输出传递给某个后续命令 - 尽管它与您设想的方法不同。

In the circumstance that the output of myprog (perhaps more aptly notmyprog ) is too big to write to disk, this approach would not be good. myprog的输出(可能更适合notmyprog )太大而无法写入磁盘的情况下,这种方法不会很好。

A solution that cleans up the temp file in the same line and still pipes the contents out at the end would be this 一个解决方案,清除同一行中的临时文件,并仍然在最后管出内容将是这个

myprog -o tmpfile input_file && contents=`cat tmpfile` && rm tmpfile && echo "$contents"

Which stores the contents of the file in a variable so that it may be accessed after deleting the file. 它将文件的内容存储在变量中,以便在删除文件后访问它。 Note the quotes in the argument of the echo command. 请注意echo命令参数中的引号。 These are important to preserve newlines in the file contents. 这些对于保留文件内容中的换行很重要。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM