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.