需要有关如何转义此perl命令的帮助

时间:2013-06-09 01:25:35

标签: perl escaping command system

我试图在perl中执行此命令,它在bash中工作正常。但是从perl执行时,它不起作用。也许我没有逃过正确的角色?能帮忙吗?

my $comp_command = "./jnx_comp.py <(/usr/bin/ssh $boss\@$ftpServer[$j] '/bin/cat $compList[0]') <(/usr/bin/ssh $boss\@$ftpServer[$j] '/bin/cat $compList[1]')"

my $result = `$comp_command`;

以下是运行脚本时给出的错误:

sh: -c: line 0: syntax error near unexpected token `('
sh: -c: line 0: `./jnx_comp.py <(/usr/bin/ssh jnxapps@dcsftp01n '/bin/cat /home/A11256/out/recon/JNX_EOD_20130606.csv'

1 个答案:

答案 0 :(得分:5)

使用bash执行时效果很好,因为它是有效的bash命令。

使用 Perl sh执行时无法正常工作,因为它不是有效的sh命令。

如果要执行bash命令,则需要实际执行bash

my $result = `bash -c bash_commmand_here`;

让我们解决一些插值问题。

use String::ShellQuote qw( shell_quote );

my $remote_cmd1 = shell_quote('/bin/cat', $compList[0]);
my $remote_cmd2 = shell_quote('/bin/cat', $compList[1]);

my $ssh_cmd1 = shell_quote('/usr/bin/ssh', "$boss\@$ftpServer[$j]", $remote_cmd1);
my $ssh_cmd2 = shell_quote('/usr/bin/ssh', "$boss\@$ftpServer[$j]", $remote_cmd2);

my $bash_cmd = "./jnx_comp <( $ssh_cmd1 ) <( $ssh_cmd2 )";

my $sh_cmd = shell_quote('bash', '-c', $bash_cmd);

`$sh_cmd`