简体   繁体   English

STDOUT从外部重定向,在控制台上看不到任何输出

[英]STDOUT redirected externally and no output seen at the console

I have a program which reads the output from external application.The external app gives set of output. 我有一个程序可以读取外部应用程序的输出。外部应用程序提供一组输出。 My program reads the output from this external app while($line=<handle to external app>) and print it to STDOUT.But the " print $line STDOUT " prints only some lines and when the error has occurred ,print to STDOUT is not working , but my one more logging statement " push @arr,$line " has stored complete output from the external app.From this i got to know STDOUT is not working properly when error happens. 我的程序同时读取了此外部应用程序的输出while($line=<handle to external app>)并将其打印到STDOUT。但是“ print $line STDOUT ”仅打印一些行,并且当发生错误时,打印到STDOUT的结果是不能正常工作,但是我的另一个日志记录语句“ push @arr,$line ”已经存储了来自外部应用程序的完整输出。由此我知道发生错误时STDOUT不能正常工作。

Eg: if external app output is like: 例如:如果外部应用程序输出类似于:


Starting command
First command executed successfully    
Error:123 :next command failed    
Program terminated

In here the STDOUT prints only : STDOUT在这里仅打印:

Starting command
First command executed successfully

But if I check the array it has complete output including error details. 但是,如果我检查数组,则它具有包括错误详细信息的完整输出。 So I guessed STDOUT has been redirected or lost. 所以我猜STDOUT已被重定向或丢失。

So I tried storing STDOUT in the beginning of the program to $old_handle using open and then try to restore it before print statement using select($old_handle) (thinking some thing redirects STDOUT when error happens) 因此,我尝试$old_handle using open将程序开头的STDOUT存储到$old_handle using ,然后尝试使用select($old_handle)在print语句之前将其还原(考虑到发生错误时某些事情会将STDOUT重定向)

But I was not successfull, I don't know what is wrong here. 但是我没有成功,我不知道这里出了什么问题。 Please help me. 请帮我。

It's possible the output is being buffered. 输出可能被缓冲。 Try setting 尝试设定

$| = 1;

at the start of your program. 在程序开始时。 This will cause the output to be displayed straight away, rather than being buffered for later. 这将导致输出立即显示,而不是被缓冲以备后用。

Just guess, may be because error output doesn't go to STDOUT. 只是猜测,可能是因为错误输出没有转到STDOUT。 Use redirect 使用重定向

first_program |& perl_program

or 要么

first_program 2>&1 | perl_program

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

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