如何在linux中查看stderr输出

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/15344547/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-06 19:14:06  来源:igfitidea点击:

How to see stderr output in linux

linuxperl

提问by adrian4aes

In a script in perl I have the following:

在 perl 的脚本中,我有以下内容:

print STDERR "Access error"

I would like know where this message is printed, but I don't know how can I see the Standar error output in LInux.

我想知道这条消息的打印位置,但我不知道如何在 LInux 中看到标准错误输出。

采纳答案by Quentin

It is printed to wherever standard error is set to for your environment.

它会打印到为您的环境设置标准错误的任何位置。

If you are running it from a console, then it will be mixed in with the standard output and displayed on the console (it won't be redirected if you redirect STDOUT, you have to redirect it separately).

如果您从控制台运行它,那么它将与标准输出混合并显示在控制台上(如果您重定向 STDOUT,它将不会被重定向,您必须单独重定向它)。

If you are running it from CGI under Apache, then it will be dropped into your error.log file (wherever Apache is configured to save that).

如果您在 Apache 下从 CGI 运行它,那么它将被放入您的 error.log 文件(无论 Apache 配置为保存该文件的位置)。

If you are running it from somewhere else… well an exhaustive list is out of scope for Stackoverflow so you should try asking a more specific question ;)

如果您从其他地方运行它……那么详尽的列表超出了 Stackoverflow 的范围,因此您应该尝试提出更具体的问题;)

Example of where it might be directed to at the console:

它可能被定向到控制台的示例:

david@raston err $ cat err.pl
#!/usr/bin/env perl

use strict;
use warnings;
use v5.16;

say "out";
say STDERR "error";
~/tmp/err :
david@raston err $ ./err.pl
out
error
~/tmp/err :
david@raston err $ ./err.pl > stdout
error
~/tmp/err :
david@raston err $ ./err.pl 2> stderr
out

回答by Gilles Quenot

Both the standard (STDOUT) and the error output (STDERR) are displayed on your (pseudo) terminal.

标准 ( STDOUT) 和错误输出 ( STDERR) 都显示在您的(伪)终端上。

If you want to trace the outputs :

如果要跟踪输出:

error log :

错误日志:

./script.pl 2> err.log

standard output log :

标准输出日志:

./script.pl > out.log

both STDERRand STDOUTin the same file :

双方STDERRSTDOUT在同一文件中:

./script.pl > out.log 2>&1

or with bash:

或使用bash

./script.pl &> out.log

A good tutorial

很好的教程

回答by Gilles Quenot

You need to run the Perl script in a terminal. Depending on whether you have X on your system or not, you could use xtermor you could use a virtual console (tty1-7) to run your script. Both stderrand stdoutare connected to these devices.

您需要在终端中运行 Perl 脚本。根据您的系统上是否有 X,您可以使用xterm或使用虚拟控制台 ( tty1-7) 来运行您的脚本。两者stderrstdout连接到这些设备。

回答by ikegami

Normally, STDOUT and STDERR are both output to your terminal.

通常,STDOUT 和 STDERR 都会输出到您的终端。

$ perl -e'print "foo\n"; print STDERR "bar\n";'
foo
bar

But it's possible to redirect either and both.

但是可以重定向一个或两个。

$ perl -e'print "foo\n"; print STDERR "bar\n";' 2>stderr.log
foo

$ cat stderr.log
bar

For example, the data sent to STDERR by a CGI script usually ends up in log file specified in the web server's configuration.

例如,由 CGI 脚本发送到 STDERR 的数据通常会在 Web 服务器配置中指定的日志文件中结束。

It's possible for a program to get information about STDERR on a linux system.

程序可以在 linux 系统上获取有关 STDERR 的信息。

$ perl -e'system "ls -l /proc/$$/fd"' 2>stderr.log |cat
total 0
lrwx------ ... 0 -> /dev/pts/1
l-wx------ ... 1 -> pipe:[210178663]            <-- STDOUT is a pipe
l-wx------ ... 2 -> /home/ikegami/stderr.log    <-- STDERR is a file
lr-x------ ... 3 -> pipe:[210178668]