Completely rewrote this answer. Original blockquoted below.

Running your second code set (the one that doesn't work), I think you have a typo or two:

#!/usr/bin/perl use strict; use warnings; # Assumptions my $dir = '/home'; print "\$dir = [$dir]\n"; my $file = 'test.dat'; print "\$file = [$file]\n"; # Proceed my $regexpaaa=qq/-i -e \'\^AAA=\'/ -e \'\^XXXXX\ AAA=\'/; print "\$regexpaaa = [$regexpaaa]\n"; my @command2 = ("ssh us@pp -o BatchMode=yes grep $regexpaaa $dir/$file +"); print "\@command2:\n"; my $command2idx = 0; foreach my $command2 (@command2) { print " [$command2idx] = [$command2]\n"; $command2idx++; } exit;

Won't even run:

S:\Steve\Dev\PerlMonks\P-2017-06-08@1920-ssh-grep>perl grep2.pl syntax error at grep2.pl line 13, near "qq/-i -e \'\^AAA=\'/ -e " Can't find string terminator "'" anywhere before EOF at grep2.pl line +13. S:\Steve\Dev\PerlMonks\P-2017-06-08@1920-ssh-grep>

This is why you should post a Short, Self-Contained, Correct Example.

Hint: You also have an error on the @command2 =line. Pay attention to what is different between that line in your first example and your second.


Original answer left for reference:

Adjusted your code to show what it's doing (with some assumptions added):

#!/usr/bin/perl use strict; use warnings; # Assumptions my $dir = '/home'; print "\$dir = [$dir]\n"; my $file = 'test.dat'; print "\$file = [$file]\n"; # Proceed my $regexpaaa=qq/-i -e \'\^AAA=\'/; print "\$regexpaaa = [$regexpaaa]\n"; my @command2 = ("ssh us\@ip -o BatchMode=yes grep $regexpaaa $dir/$fil +e"); print "\@command2:\n"; my $command2idx = 0; foreach my $command2 (@command2) { print " [$command2idx] = [$command2]\n"; $command2idx++; } exit;

Then ran it:

S:\Steve\Dev\PerlMonks\P-2017-06-08@1920-ssh-grep>perl grep1.pl $dir = [/home] $file = [test.dat] $regexpaaa = [-i -e '^AAA='] @command2: [0] = [ssh us@ip -o BatchMode=yes grep -i -e '^AAA=' /home/test.da +t] S:\Steve\Dev\PerlMonks\P-2017-06-08@1920-ssh-grep>

I presume you send @command2to the shell for execution and trap the feedback.

Looks to me like maybe your sshcommand might need some quote encapsulation -- but I'm no Linux expert, and, as noted above, that's not a Perl issue.


In reply to Re: perl, ssh and grep by marinersk
in thread perl, ssh and grep by hveneticus

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.