开发者

Inability to capture output from "dd" using backticks

开发者 https://www.devze.com 2023-03-30 04:27 出处:网络
I\'ve been working with a very old Solaris system and not in a position to add more modules to make my life easier, and I\'m working with a number of scripts that use various command line options.

I've been working with a very old Solaris system and not in a position to add more modules to make my life easier, and I'm working with a number of scripts that use various command line options.

The majority of what I'm working on is, in fact working, but I'm coming up with something that I just can't seem to get round.

I'm pulling data from a tape using the "dd" command and need to capture the output to ascertain if I'm experiencing any tape read errors.

("comment()" is a subroutine that I've already created)

#!/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

When the script runs, I see "123+0 records in, 123+0 records out" being posted to the terminal screen, but my loop where the @tapeinfo doesn't seem to be testing against at all. I don't get either an error or a logging of the information.

Am 开发者_如何学运维I missing something dreadfully simple here??


dd outputs to stderr while backticks capture stdout. This is documented in perlop:

Because backticks do not affect standard error, use shell file descriptor syntax (assuming the shell supports this) if you care to address this. To capture a command's STDERR and STDOUT together:

$output = `cmd 2>&1`;

You can do:

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


You could use strace or whatever that system provides to find out definitely, but the two likely options are

  • The output is sent to STDERR. You could address this by merging STDERR and STDOUT (2>&1).
  • The output is sent to the tty. Not much can be done about that.
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号