将文件重定向或通过管道传输到标准输出

发布于 2024-09-28 10:34:07 字数 166 浏览 5 评论 0原文

如何重定向一个小(<20k)文件以通过管道传输到另一个程序,以便它永远不会写入光盘。

您可以使用 cfront 和 gcc 作为示例。只要您将通常输出文件的内容重定向到通常接受文件的内容,任何示例都可以。

您可以使用任何脚本或 shell,但我更喜欢看到 bash 或 perl。

How do u redirect a small(<20k) file to be piped to another program, so that it is never written to disc.

You could use cfront and gcc as an example. Any example will do as long as you are redirecting something that normally outputs a file to something that usually accepts a file.

You could could use any script or shell, but I would prefer to see bash or perl.

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

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

发布评论

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

评论(3

总以为 2024-10-05 10:34:07

有趣的!我尝试了这个:

$ mkfifo fifo.txt
$ curl http://www.google.com -o fifo.txt | wc fifo.txt

几乎是一个管道...一个命名管道:p

另一方面,如果您有兴趣在标准输出上打印它(无需管道):

$ curl http://www.google.com -o `tty`

interesting! I tried this:

$ mkfifo fifo.txt
$ curl http://www.google.com -o fifo.txt | wc fifo.txt

almost a pipe ... a named pipe :p

On the other hand if all you are interested in print it on stdout (without having to pipe):

$ curl http://www.google.com -o `tty`
金兰素衣 2024-10-05 10:34:07

如果一个程序期望从文件中读取它的输入,那么这几乎就是您必须要做的。为了避免接触文件系统,您使用的所有程序都必须了解如何从 stdin 读取和写入 stdout(或其他非文件系统的设备)。

Unix 世界中的许多程序都能够像读取文件一样轻松地读取和写入 std{in,out}。一个很好的例子是 gnu tar 和 gzip。 tar 可以写入标准输出并将输出直接通过管道传送到 gzip:


tar cf-foo/| gzip -c > > foo.tgz

但这需要 tar 和 gzip 都能够读取/写入 stdin/stdout 以及常规文件。

如何在自己的程序中实现这一点取决于所涉及的语言,但在大多数情况下处理 stdout & stderr 与任何其他文件几乎相同。您的命令行参数应该允许用户选择此选项。

If a program is expecting to read it's input from a file then that's pretty much what you're going to have to do. To avoid touching the file system, all the programs you are using have to understand how to read from stdin and write to stdout (or some other device that's not a file system).

Many programs in the unix universe are capable of reading from and writing to std{in,out} just as easily as they are to a file. A good example is gnu tar and gzip. It's possible for tar to write to stdout and to pipe that output directly into gzip:


tar cf - foo/ | gzip -c > foo.tgz

but that requires both tar and gzip to be able to read/write to stdin/stdout as well as regular files.

How you achieve this in your own program depends on the language involved but in most cases handling stdout & stderr is pretty much the same as any other file. Your command line arguments should allow the user to choose this as an option.

自演自醉 2024-10-05 10:34:07

如果程序A要运行程序B,可以使用open< /a> 函数在 A 和 B 的 STDIN 之间创建匿名管道:

program A.pl:

#!/usr/bin/perl

use strict;
use warnings;

my $pid = open my $pipe, "|-", "./B.pl"
    or die "could not run B.pl: $!";

for my $i ("a" .. "g") {
    print $pipe "$i\n";
}
close $pipe;

waitpid $pid, 0;

program B:

#!/usr/bin/perl

use strict;
use warnings;

my $i = 0;
while (my $line = <>) {
    print $i++, ": $line";
}

If program A is going to run program B, you can use the open function to create an anonymous pipe between A and B's STDIN:

program A.pl:

#!/usr/bin/perl

use strict;
use warnings;

my $pid = open my $pipe, "|-", "./B.pl"
    or die "could not run B.pl: $!";

for my $i ("a" .. "g") {
    print $pipe "$i\n";
}
close $pipe;

waitpid $pid, 0;

program B:

#!/usr/bin/perl

use strict;
use warnings;

my $i = 0;
while (my $line = <>) {
    print $i++, ": $line";
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文