为什么exec-ing gtar会挂起我的Perl程序?

Ale*_*ong 1 unix perl gzip

使用gtar将额外文件添加到现有存档时,循环会过早终止或挂起.它也会在创建初始tar.gz文件后终止.

但是,如果我从循环中删除了gtar调用并将print语句放在它们的位置,则循环按预期执行.有人知道为什么吗?下面是循环中包含的代码.

if (-e "flex_$yearA"."_"."$monthA.tar.gz")
{ print"accessing Flex tar \n";
 exec "gtar --append --file=flex_$yearA"."_"."$monthA.tar.gz $FILE";
}
else
{ print "creating Flex Tar \n ";
 exec "gtar -cvsf flex_$yearA"."_"."$monthA.tar.gz $FILE"; 
}
Run Code Online (Sandbox Code Playgroud)

Ovi*_*vid 5

你想要"系统",而不是"exec".这是一个更清洁的版本:

my $tarball = "flex_${yearA}_${monthA}.tar.gz";

if ( -e $tarball ) { 
    print"accessing Flex tar \n";

    my $command = "gtar --append --file=$tarball $FILE";
    system($command) == 0
      or die "Could not ($command): $?";
}
else{ 
    print "creating Flex Tar \n ";
    my $command =  "gtar -cvsf $tarball $FILE";
    system($command) == 0
      or die "Could not ($command): $?";
}
Run Code Online (Sandbox Code Playgroud)

但是,我想知道所有这些变量来自哪里.你可能会在这里暴露一个严重的安全漏洞 有关将列表传递给系统(更安全)的更多信息,请阅读"perldoc -f system".