将管道输入转储到文件的简单方法是什么? (Linux)

发布于 2024-07-04 04:28:49 字数 74 浏览 11 评论 0原文

我正在寻找一个小 shell 脚本,它可以将任何内容通过管道传输到其中,并将其转储到文件中..用于电子邮件调试目的。 有任何想法吗?

I'm looking for a little shell script that will take anything piped into it, and dump it to a file.. for email debugging purposes. Any ideas?

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

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

发布评论

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

评论(11

天煞孤星 2024-07-11 04:28:49

unix 命令 tee 可以做到这一点。

man tee

The unix command tee does this.

man tee
浮华 2024-07-11 04:28:49

啊? 我想,我没听懂这个问题?

难道你不能将管道结束为 >> 将

例如,

echo "Foobar" >> /home/mo/dumpfile

Foobar 附加到转储文件(并在必要时创建转储文件)。 不需要 shell 脚本...这是您正在寻找的吗?

Huh? I guess, I don't get the question?

Can't you just end your pipe into a >> ~file

For example

echo "Foobar" >> /home/mo/dumpfile

will append Foobar to the dumpfile (and create dumpfile if necessary). No need for a shell script... Is that what you were looking for?

夏有森光若流苏 2024-07-11 04:28:49

如果你不关心输出结果

cat - > filename

或者

cat > filename

if you don't care about outputting the result

cat - > filename

or

cat > filename
剩一世无双 2024-07-11 04:28:49

如果你想在脚本中分析它:

while /bin/true; do
    read LINE
    echo $LINE > $OUTPUT
done

但是你可以简单地使用cat。 如果 cat 在标准输入上获取某些内容,它会将其回显到标准输出,因此您必须将其通过管道传输到 cat >$OUTPUT。 这些也会做同样的事情。 第二个也适用于二进制数据。

If you want to analyze it in the script:

while /bin/true; do
    read LINE
    echo $LINE > $OUTPUT
done

But you can simply use cat. If cat gets something on the stdin, it will echo it to the stdout, so you'll have to pipe it to cat >$OUTPUT. These will do the same. The second works for binary data also.

鱼忆七猫命九 2024-07-11 04:28:49

如果您想要 shell 脚本,请尝试以下操作:

#!/bin/sh
exec cat >/path/to/file

If you want a shell script, try this:

#!/bin/sh
exec cat >/path/to/file
成熟的代价 2024-07-11 04:28:49

如果 exim 或 sendmail 是写入管道的内容,那么 procmail 是一个很好的答案,因为它会给您文件锁定/序列化,并且您可以将它们全部放在同一个文件中。

如果你只想写入一个文件,那么
- 三通 > /tmp/log.$$
或者
- 猫> /tmp/log.$$
可能就足够好了。

If exim or sendmail is what's writing into the pipe, then procmail is a good answer because it'll give you file locking/serialization and you can put it all in the same file.

If you just want to write into a file, then
- tee > /tmp/log.$$
or
- cat > /tmp/log.$$
might be good enough.

橘虞初梦 2024-07-11 04:28:49

您并不是唯一需要类似功能的人......事实上,几十年前就有人想要该功能并开发了 tee :-)

当然,您可以使用 >tee 将 stdout 直接重定向到任何 shell 中的文件 特点:

echo "hello, world!" > the-file.txt

You're not alone in needing something similar... in fact, someone wanted that functionality decades ago and developed tee :-)

Of course, you can redirect stdout directly to a file in any shell using the > character:

echo "hello, world!" > the-file.txt
树深时见影 2024-07-11 04:28:49
cat > FILENAME
cat > FILENAME
不忘初心 2024-07-11 04:28:49

标准的 UNIX 工具 Tee 可以做到这一点。 它将输入复制到输出,同时还将其记录到文件中。

The standard unix tool tee can do this. It copies input to output, while also logging it to a file.

恍梦境° 2024-07-11 04:28:49

使用Procmail。 Procmail 是你的朋友。 Procmail 就是为这类事情而设计的。

Use Procmail. Procmail is your friend. Procmail is made for this sort of thing.

玩套路吗 2024-07-11 04:28:49

使用 <>; | tee <> 用于将命令<>通过管道传输到文件<>

这也将显示输出。

Use <<command>> | tee <<file>> for piping a command <<command>> into a file <<file>>.

This will also show the output.

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