0

I have a bash command I'm executing in perl as follows

 @args = ( "bash" , "-c",  "diff <(ssh  -o \"StrictHostKeyChecking no\" user-db01 'mysql -uuser -ppass --execute=\"show databases\"') <(ssh  -o \"StrictHostKeyChecking no\" user-db02 'mysql -uuser -ppass --execute=\"show databases\"')");
system(@args);

but this doesn't allow me to get the output to a string variable. If I execute the exact samething with backticks

 $cmd = "bash -c diff <(ssh  -o \"StrictHostKeyChecking no\" user-db01 'mysql -uuser -ppass --execute=\"show databases\"') <(ssh  -o \"StrictHostKeyChecking no\" user-db02 'mysql -uuser -ppass --execute=\"show databases\"')";
 my $res = `$cmd`;  

its throwing an error sh: -c: line 0: syntax error near unexpected token('` What can I do to execute the command and get the result in a variable?

1
  • 1
    What is the sample code and the exact error from backticks? Commented Aug 14, 2012 at 13:19

1 Answer 1

2

You can use one of the usual CPAN modules, e.g. IPC::System::Simple that has a capture() method; or a more-likely-to-be-present IPC::Open2.

Your backticsk problem is that you didn't quote the diff command, thus making it an argument to bash:

BREAKS: bash -c diff <( cmd2 ) <( cmd1 )
GOOD  : bash -c "diff <( cmd2 ) <( cmd1 )"

Your code treats every "word" as a separate argument to your system shell (NOT to bash), e.g. it's the same as calling system("bash", "-c", "diff", " <(ssh" ...)

Sign up to request clarification or add additional context in comments.

2 Comments

When I do a $cmd = "cmd" and then backtick $cmd, the double quotes within cmd are causing problems even if they are escaped
@Aks - you need to escape the escapes, most likely. A highly annoying excercise, so you should look into CPAN options in my answer.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.