Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Unable to execute ssh command containing parentheses with perl: "syntax error near unexpected token `('"

If I run this command from the command line, it works as expected on the remote server:

ssh admin@example.com "docker exec -it -d tasks_live_work_ec2_test_server /bin/sh -c \"/usr/bin/nvim -c 'silent! call SetupInstantServer()'\""

However, if I try to execute it from a perl script, I get errors:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

    my $cmd = qq|docker exec -it -d ${image_name}_server /bin/sh -c \"/usr/bin/nvim -c 'silent! call SetupInstantServer()'\"|;
    `ssh admin\@example.com "$cmd"`;

bash: -c: line 1: syntax error near unexpected token '('

Escaping the parens with backslashes suppresses the error, but the SetupInstantServer function in vim never gets called.

>Solution :

What I would do, using 2 here-doc:

#!/usr/bin/perl

system<<PerlEOF;
    ssh admin\@example.com<<ShellEOF
    docker exec -it -d ${image_name}_server /bin/sh -c "/usr/bin/nvim -c 'silent! call SetupInstantServer()'"
ShellEOF
PerlEOF

You can decide to add quotes on a ‘HereDoc’ to prevent shell expansion or the need to escape @. Up to your needs.

Check perldoc perlop#Quote-and-Quote-like-Operators

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading