首页 文章

用Tee以错误的顺序打印Perl STDERR

提问于
浏览
0

我正在尝试将STDOUT和STDERR从perl脚本(从bash脚本执行)重定向到屏幕和日志文件 .

perlscript.pl

#!/usr/bin/perl

print "This is a standard output";
print "This is a second standard output";
print STDERR "This is an error";

bashscript.sh

#!/bin/bash

./perlscript.pl 2>&1 | tee -a logfile.log

如果我直接执行perlscript,屏幕输出将以正确的顺序打印:

This is a standard output
This is a second standard output
This is an error

但是当我执行bash脚本时,首先打印STDERR(在屏幕和文件中):

This is an error
This is a standard output
This is a second standard output

使用bash脚本作为子项,输出完美无缺 . 这是perl或tee的错误吗?难道我做错了什么?

2 回答

  • 3

    关闭缓冲的常用技巧是设置变量 $| . 在脚本开头添加以下行 .

    $| = 1;
    

    这会使缓冲关闭 . 另请参阅MJD的这篇优秀文章,解释perl中的缓冲 . Suffering from Buffering?

  • 4

    我想这与STDOUT和STDERR缓冲区的刷新方式有关 . 尝试

    autoflush STDOUT 1;
    

    在perl脚本的开头,以便在每个print语句之后刷新STDOUT .

相关问题