如何将STDOUT重定向到Perl脚本中的文件后还原?

时间:2010-04-06 14:03:42

标签: perl

以下是我的代码的一部分,我的代码使用$value =1输入“if loop”,并且进程iperf.exe的输出为 进入my_output.txt。当我在alarm(20sec)时间之后超时时,也只想捕获此过程的输出。

然后我想继续命令提示符,但我无法返回命令提示符。

不仅此代码本身在命令提示符下不是PRINT,而是在my_output.txt文件上打印

(我正在循环这个 if block 通过我的其余代码)

output.txt的

inside value loop2
------------------------------------------------------------
Server listening on UDP port 5001
Receiving 1470 byte datagrams
UDP buffer size: 8.00 KByte (default)
------------------------------------------------------------
[160] local 10.232.62.151 port 5001 connected with 10.232.62.151 port 1505
[ ID] Interval       Transfer     Bandwidth       Jitter   Lost/Total Datagrams
[160]  0.0- 5.0 sec  2.14 MBytes  3.59 Mbits/sec  0.000 ms    0/ 1528 (0%)
inside value loop3
clue1
clue2
inside value loop4
one iperf completed
Transfer

Transfer Starting: Intent { act=android.settings.APN_SETTINGS }

******AUTOMATION COMPLETED******

在重新初始化STDOUT时看起来有些问题。

我尝试使用close(STDOUT);,但又没有返回STDOUT

代码

if($value)
{    
my $file = 'my_output.txt';

use Win32::Process;

print"inside value loop\n";
# redirect stdout to a file

open STDOUT, '>', $file
  or die "can't redirect STDOUT to <$file> $!";

Win32::Process::Create(my $ProcessObj,
                       "iperf.exe",
                       "iperf.exe -u -s -p 5001",
                       0,
                       NORMAL_PRIORITY_CLASS,
                       ".") || die ErrorReport();

$alarm_time = $IPERF_RUN_TIME+2; #20sec            

print"inside value loop2\n";
sleep $alarm_time;    

$ProcessObj->Kill(0);

sub ErrorReport{
    print Win32::FormatMessage( Win32::GetLastError() );
}

print"inside value loop3\n";
print"clue1\n";
#close(STDOUT);
print"clue2\n";
print"inside value loop4\n";
print"one iperf completed\n";
}

my $data_file="my_output.txt";

open(ROCK, $data_file)|| die("Could not open file!");
@raw_data=<ROCK>; 

@COUNT_PS =split(/ /,$raw_data[7]);
my $LOOP_COUNT_PS_4 = $COUNT_PS[9];
my $LOOP_COUNT_PS_5 = $COUNT_PS[10];

print "$LOOP_COUNT_PS_4\n";
print "$LOOP_COUNT_PS_5\n";

my $tput_value = "$LOOP_COUNT_PS_4"." $LOOP_COUNT_PS_5";
print "$tput_value";
close(ROCK);
print FH1 "\n $count    \| $tput_value \n";

3 个答案:

答案 0 :(得分:6)

在重新打开之前,您需要保存STDOUT

open(SAVE, ">&STDOUT") || die "Can't save STDOUT\n"; 
...
open(STDOUT, ">&SAVE") || die "Can't restore STDOUT\n"; 

您还可以使用动态变量来“保存”STDOUT(但这可能需要重新组织您的代码):

do {
  local *STDOUT;
  open(STDOUT, "...");
  ...
}; # STDOUT is restored here

最后,您可以使用反引号来捕获完整的输出:

$x = `iperf.exe ...`;

答案 1 :(得分:5)

使用local

{
    local *STDOUT;
    open STDOUT, '>', $file
        or die "can't redirect STDOUT to <$file> $!";

    Win32::Process::Create(my $ProcessObj,
                       "iperf.exe",
                       "iperf.exe -u -s -p 5001",
                       0,
                       NORMAL_PRIORITY_CLASS,
                       ".") || die ErrorReport();

    $alarm_time = $IPERF_RUN_TIME+2; #20sec            

    print"inside value loop2\n";
    sleep $alarm_time;
    $ProcessObj->Kill(0);
}

答案 2 :(得分:2)

您也可以尝试select

   open FF,">$filename" or die "error opening $filename for writing: $!";
   print "hi 1\n"; # this goes to stdout
   ...
   select FF;    # sets output to FF
   print "hi 2\n"; # this goes to FF ($filename )
   ...
   select STDOUT; # resets output to STDOUT
   print "hi 3\n"; # this goes to stdout