【发布时间】:2015-03-20 08:27:39
【问题描述】:
我有一个 Perl 脚本 progA.pl,它需要使用 system 命令运行另一个 Perl 脚本 progB.pl。但是,progB.pl 已在 ~/.bashrc 中使用别名,因此我需要确保它在 ~/.bashrc 加载后运行。我可以通过使用bash 和-lc 选项来实现这一点。
对于这个问题,我尽可能简化问题,考虑以下版本的progB.pl
use feature qw(say);
use strict;
use warnings;
use Data::Dump qw(dd dump);
say "Received \@ARGV: " . dump @ARGV;
这里是progA.pl:
use feature qw(say);
use strict;
use warnings;
use Data::Dump qw(dd dump);
my $cmd = qq(progB.pl --opt='This option contains '"'"'single'"'"' quotes');
say "cmd = " . dump($cmd);
system( "$cmd" );
say "-----";
system( 'bash -c ' . "$cmd" );
say "-----";
system( 'bash -c ' . "'$cmd'" );
say "-----";
system( "bash -c \"$cmd\"" );
跑步
$ progA.pl
给出输出:
cmd = "progB.pl --opt='This option contains '\"'\"'single'\"'\"' quotes'"
Received @ARGV: "--opt=This option contains 'single' quotes"
-----
Received @ARGV: ()
-----
Received @ARGV: "--opt=This"
-----
Received @ARGV: "--opt=This option contains single quotes"
当progB.pl 直接运行而不使用bash -c 时,我们看到这工作正常。当我使用bash -c 运行命令时,三个选项都没有正常工作。
如何使用包含单引号的参数运行progB.pl,同时使用bash -c?
【问题讨论】:
-
创建 perl 的原因之一是对多个递归 bash 调用以及您正在经历的引用地狱的反应。如果可以选择,我建议将 bash 脚本重写为 perl 模块...
-
@DovGrobgeld 好建议,但我认为这不是这种情况的选择..