我应该向 STDERR 或 STDOUT 输出警告吗?

发布于 2024-08-05 16:58:08 字数 128 浏览 3 评论 0原文

我正在制作一个脚本来处理一组预定义的数据,并将其输出到文件。当一个数据(在我有权访问的每组数据中始终是“常规”)不同时,我想弹出一条警告,表明该值未处理(因为我不知道它如何影响数据)。我应该将此警告输出到 stderr 或 stdout 吗?

I'm making a script that handles a predefined set of data, outputting to a file. I want to pop up a warning when one datum (which is always "Regular" in every set that I've had access to) is different stating that this value is unhandled (since I don't know how it affects the data). Should I output this warning to stderr or stdout?

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

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

发布评论

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

评论(3

等待我真够勒 2024-08-12 16:58:08

如果我保存此脚本的输出(即仅 stdout)以便稍后处理它,该警告是否会干扰输出的解析方式?此外,如果输出通过管道传输到另一个进程,则警告应该显示在终端上,以便用户立即看到它。

由于这些原因,通常,您会向 stderr 输出警告。

If I save the output of this script (i.e. stdout only) so that I could process it later, would that warning interfere with how the output is parsed? Moreover, if output is piped to another process, the warning should show up on the terminal, so the user sees it immediately.

For those reasons, in general, you output warnings to stderr.

泼猴你往哪里跑 2024-08-12 16:58:08

警告应该发送到 stderr。

除了其他人提出的观点(导致下游进程解析错误并在控制台向用户隐藏错误)之外,还有一个灵活性问题。

如果用户不希望来自 stderr 的警告进入正在解析 stdout 的下游进程,则他们不必执行任何特殊操作。

your_script | downstream_process

如果用户希望来自 stderr 的警告转到将解析 stdout 和 stderr 的下游进程,则用户可以使用 2>&1 将 stderr 重定向到 stdout。

your_script 2>&1 | downstream_process

如果将警告和任何正常数据都输出到标准输出,则用户没有很好的方法将警告与数据分开而不解析所有内容。
因此,将警告发送到 stderr 也可以让您的脚本更加灵活。

The warning should go to stderr.

In addition to the points presented by others (causing parsing errors for downstream processes and hiding the error from the user at the console), there is an issue of flexibility.

If the user does not want the warning from stderr to go to a downstream process that is parsing stdout, they don't have to do anything special.

your_script | downstream_process

If the user wants the warning from stderr to go to a downstream process that will parse stdout and stderr, the user can use 2>&1 to redirect stderr into stdout.

your_script 2>&1 | downstream_process

If you output both warnings and any normal data to stdout, the user has no good way of separating the warnings from the data without parsing everything.
So sending the warnings to stderr gives your script more flexibility as well.

初心 2024-08-12 16:58:08

真正的问题是:如果有人将脚本的输出重定向到文件,您是否希望将警告放置在文件中,或者定向到用户?

如果您希望用户因警告而采取某些操作,则应将其发送到 STDERR。如果某些下游脚本可能因警告而出错,则应转到 STDERR。

The real question is: if someone were to redirect the output of your script to a file, would you want the warning placed in the file, or directed to the user?

If you're expecting the user to take some action as a result of the warning, it should go to STDERR. If some downstream script is likely to be tripped up by the warning, it should go to STDERR.

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