在perl脚本中执行unix命令

时间:2012-03-12 20:10:51

标签: perl unix variables command execute

如何在ticks中使用以下外部命令代替变量?或类似的东西? sed -i.bak -e '10,16d;17d' $docname; (这有效)

即。 sed -i.bak -e '$line_number,$line_end_number;$last_line' $docname;

3 个答案:

答案 0 :(得分:2)

my $result = 
      qx/sed -i.bak -e "$line_number,${line_end_number}d;${last_line}d" $docname/;

线分割时避免SO上的水平滚动条;否则,它将在一条线上。

或者,因为不清楚是否有任何输出要捕获:

system "sed -i.back '$line_number,${line_end_number}d;${last_line}d' $docname";

或者你可以自己将其分成参数:

system "sed", "-i.back", "$line_number,${line_end_number}d;${last_line}d", "$docname";

这往往更安全,因为shell没有机会干扰对参数的解释。

答案 1 :(得分:0)

@args = ("command", "arg1", "arg2");
system(@args) == 0 or die "system @args failed: $?"

此外,在手册上:

perldoc -f system

答案 2 :(得分:0)

我认为你应该阅读qq用于字符串here

你可能想要这样的东西:

use strict;
use warnings;

my     $line_number = qq|10|;
my $line_end_number = qq|16d|;
my       $last_line = qq|17d|;
my        $doc_name = qq|somefile.bak|;
my     $sed_command = qq|sed -i.bak -e '$line_number,$line_end_number;$last_line' $doc_name;|;

print $sed_command;
qx|$sed_command|;
相关问题