Home > OS >  Unable to execute ssh command containing parentheses with perl: "syntax error near unexpected t
Unable to execute ssh command containing parentheses with perl: "syntax error near unexpected t

Time:12-30

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

ssh [email protected] "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:

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.

CodePudding user response:

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

  • Related