无法使用反引号捕获"dd"的输出

1 unix perl solaris dd backticks

我一直在使用一个非常古老的Solaris系统,并且无法添加更多模块以使我的生活更轻松,而且我正在使用许多使用各种命令行选项的脚本.

我正在做的大部分工作实际上都在工作,但是我想出的东西似乎无法解决.

我正在使用"dd"命令从磁带中提取数据,并需要捕获输出以确定我是否遇到任何磁带读取错误.

("comment()"是我已创建的子程序)

#!/usr/local/bin/perl
$| = 1;     #disable output buffering

$tarfile = '/mnt/test/tmp/12345.tar';

@tapeinfo = `dd if=/dev/rmt/1cbn of=$tarfile`;

foreach(@tapeinfo){
#Check to ensure that we're not getting read errors
 $result = index($_,'read: I/O error');
 if ($result < 0){
  #No read error, log result
  comment($_);
  } else {
  # read error, terminate
  comment("Terminating due to tape read error : $_");
  last; #exit loop if error is found
  }
}
#terminate with logging
Run Code Online (Sandbox Code Playgroud)

当脚本运行时,我看到"123 + 0记录,123 + 0记录"被发布到终端屏幕,但我的循环@tapeinfo似乎根本没有测试.我没有收到错误或记录信息.

我错过了一些非常简单的东西吗?

Eug*_*ash 9

dd在反引号捕获标准输出时输出到stderr.这在perlop中有记录:

因为反引号不会影响标准错误,所以如果您需要解决此问题,请使用shell文件描述符语法(假设shell支持此语法).要一起捕获命令的STDERR和STDOUT:

$output = `cmd 2>&1`;
Run Code Online (Sandbox Code Playgroud)

你可以做:

my @tapeinfo = qx( dd if=/dev/rmt/1cbn of=$tarfile 2>&1 );