user188276
user188276

Reputation:

need help on how to escape this perl command

i tried to execute this command in perl, it worked fine from bash. But when executing from perl, it doesn't work. Maybe I haven't escaped the right characters? Could you please help.

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`;

Here is the error it gives me when running the script:

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'

Upvotes: 0

Views: 185

Answers (1)

ikegami
ikegami

Reputation: 385849

It worked fine when executed using bash because it's a valid bash command.

It didn't work when executed using Perl sh because it's not a valid sh command.

If you want to execute a bash command, you'll need to actually execute bash.

my $result = `bash -c bash_commmand_here`;

Let's fix some interpolation problems while we're at it.

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`

Upvotes: 5

Related Questions