【发布时间】:2015-01-06 21:20:02
【问题描述】:
...另一方面,如果我在打开下一个管道之前向管道写了一些东西,这不会发生。
下面的代码应该更清楚:
sub test_concurrent_pipes
{
my $write_at_once = $_[0];
my $pipe_handle;
my @pipe_handle_list;
my $i;
foreach $i ( 1..3 )
{
open ( $pipe_handle, "| xargs echo" ) or die ( "Cannot open pipe.\n" );
if ( $write_at_once == 1 )
{
print $pipe_handle "Hello\n";
}
push( @pipe_handle_list, $pipe_handle );
}
foreach $pipe_handle ( @pipe_handle_list )
{
print $pipe_handle "world\n";
}
foreach $pipe_handle ( @pipe_handle_list )
{
close ( $pipe_handle );
}
}
print "Test 0: open all pipes before writing\n";
test_concurrent_pipes(0);
print "Test 1: write Hello before opening next pipe\n";
test_concurrent_pipes(1);
运行我得到的测试
./test_pipe_2_xargs.pl
Test 0: open all pipes before writing
world world world
Test 1: write Hello before opening next pipe
Hello
Hello
Hello world world world
正如您在测试 0 中看到的,连续打开 3 个管道而中间没有任何输出会生成 2 个空行。
奇怪的是,如果我用cat - 替换xargs echo,则不会产生空行。
xargs 的行为似乎也与其手册页相矛盾,其中指出 Blank lines on the standard input are ignored.
我怎样才能避免那些空行?
在 cygwin/XP 上的 Perl 5.14.2 和在 HP-UX 11.00 上的 Perl 5.8.8 会发生这种情况。
我最后写下我真正想做的事情,因为它在这里无关紧要:
通过 Perl 脚本有效地清理所有 Clearcase 视图中可见的所有派生对象,该脚本为每个视图派生一个进程以在从 VOB (rmdo) 中删除文件之前删除文件 (xargs rm)。
【问题讨论】:
-
很好地提供了一个完整的例子。