Perl系统命令更改变量值并抛出错误

时间:2017-04-08 03:31:40

标签: perl

我有以下脚本:

#! /bin/env perl 

use File::Basename;
my @log_files;
my $log_dir = "/tmp/";
my $path = "find_failing/";
$log_files[0] = (`find $path -name compile.log -print0`)[0];
$log_files[1] = (`find $path -name sim.log     -print0`)[0];
my $rev = "100";

print "@log_files << \n";

foreach (@log_files) {
   if (-f $_) {
      chomp;
      my $dirn = sprintf "%s/%s.%s", $log_dir, basename ($_), $rev;
      chomp $dirn;
      my $cmd = "cp -v $_ $dirn";
      chomp $cmd;
      print "C: $cmd\n";
      system ($cmd);
      system ("cp", "-v", $_, $dirn);
      system ("echo");
   }
}

当我运行时,我得到以下输出:

find_failing/simulation/tc_ctrl.rtl/compile/compile.log find_failing/simulation/tc_ctrl.rtl/test_prbs.nrz.16.prbs23/sim.log << 
C: cp -v find_failing/simulation/tc_ctrl.rtl/compile/compile.log /tmp//compile.log.100
cp: missing destination file operand after `find_failing/simulation/tc_ctrl.rtl/compile/compile.log'
Try `cp --help' for more information.
`find_failing/simulation/tc_ctrl.rtl/compile/compile.log' -> `/tmp//compile.log'

C: cp -v find_failing/simulation/tc_ctrl.rtl/test_prbs.nrz.16.prbs23/sim.log /tmp//sim.log.100
cp: missing destination file operand after `find_failing/simulation/tc_ctrl.rtl/test_prbs.nrz.16.prbs23/sim.log'
Try `cp --help' for more information.
`find_failing/simulation/tc_ctrl.rtl/test_prbs.nrz.16.prbs23/sim.log' -> `/tmp//sim.log'

命令的打印显示了我想要的变量的正确扩展。但是,当使用system()运行时,它会抛出一个错误,说明目标丢失了。

此外,当我通过将命令作为列表发出命令时,会发生复制,但目标路径缺少$ .dirn中的“。$ rev”。

我用v5.8.8和v5.24.0运行,我得到了相同的输出。

发生了什么事?为什么system()表现得这样?

谢谢。

1 个答案:

答案 0 :(得分:2)

-print0打印后跟NUL的路径,所以

  • $log_files[0]包含

    find_failing/foo/compile.log<NUL>find_failing/bar/compile.log<NUL>
    
  • $log_files[1]包含

    find_failing/foo/sim.log<NUL>find_failing/bar/sim.log<NUL>
    

替换

$log_files[0] = (`find $path -name compile.log -print0`)[0];
$log_files[1] = (`find $path -name sim.log     -print0`)[0];

( $log_files[0] ) = split /\0/, `find $path -name compile.log -print0`;
( $log_files[1] ) = split /\0/, `find $path -name sim.log     -print0`;

$log_files[0] = `find $path -name compile.log -print0`;
$log_files[1] = `find $path -name sim.log     -print0`;

s/\0.*//s for @log_files;