jhuijsing has asked for the wisdom of the Perl Monks concerning the following question:

Have a application where I need to send a command which needs to contain "\"

so the final string to send would be "blah blah item\ 0-9\ 10-14 more blah blah blah"

I have variable $object = 'item 0-9 10-14'

# command template $cmd ="blah blah $object blah blah"; ( $output, $errput ) = $ssh->capture2( $cmd ) or $logger->logdie( "Export failed: " . $ssh->error );

Replies are listed 'Best First'.
Re: Net::OpenSSH quoting problems
by salva (Canon) on May 13, 2014 at 06:33 UTC
    Net::OpenSSH can do all the quoting for you:
    ($output, $errput) = $ssh->capture('blah', 'blah', $object, 'blah', 'b +lah');
    Or if you want to keep the command template as it is:
    my $quoted_object = $ssh->shell_quote($object); $cmd = "blah blah $quoted_object blah blah"; ($output, $errput) = $ssh->capture2($cmd);
    Read the Shell quoting section on the module documentation.
      Found the problems - double faceslap 1. The squishy thing between the chair and the keyboard. incorrectly capitaltised word 2. One box I was testing on didn't have the latest software and didn't support the command I was trying to use
Re: Net::OpenSSH quoting problems
by kcott (Archbishop) on May 13, 2014 at 03:25 UTC

    G'day jhuijsing,

    You haven't actually asked a question, so I don't know exactly which part of this you're having difficulties with.

    If you just want to know how to get from 'item 0-9 10-14' to 'item\ 0-9\ 10-14', you can do it like this:

    #!/usr/bin/env perl -l use strict; use warnings; my $object = 'item 0-9 10-14'; print $object; $object =~ s/( \d+-\d+)/\\$1/g; print $object;

    Output:

    item 0-9 10-14 item\ 0-9\ 10-14

    If you wanted something else, please specify.

    -- Ken

      That doesn't work
      #with the debug flag set $Net::OpenSSH::debug |= 16; $object =~ s/( \d+-\d+)/\\$1/g; $cmd = qq|blah blah $object blah blah|; ( $output, $errput ) = $ssh->capture2( $cmd ) or $logger->logdie( "bad".$ssh->error ); using the debugger x $object item 0-7 0-9 after substitution x $object item \\ 0-7\\ 0-9' # lot of stuff removed the #open_ex ...... 'blah blah item\\ 0-7\\ 0-9
      It sending a \\ not "item\ 0-7\ 0-9"
        This works for me:
        DB<5> $object = "2-4 5-12" DB<6> $object =~ s/([-\d]+)/\\$1/g DB<7> p $object \2-4 \5-12
        Or you could store the backslash in a variable and use that variable:
        DB<11> $c = '\\'; DB<12> p $c \ DB<13> $object = "2-4 5-12" DB<14> $object =~ s/([-\d]+)/$c$1/g; DB<15> p $object \2-4 \5-12