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

Hi Monks.
First of all I have to say that I'm a perl enthusiast, but not a good developer...
Anyway, this is my question.
I have to connect to a series of machines to grep in some file a defined string, let's say "AAA=", without characters before. So I write:
... my $regexpaaa=qq/-i -e \'\^AAA=\'/; my @command2 = ("ssh us\@ip -o BatchMode=yes grep $regexpaaa $dir/$fil +e"); ...
This code works fine for me. But, for my happiness, someone put some characters (let's say: XXXXX) before "AAA=" string. So I arranged my code differently:
... my $regexpaaa=qq/-i -e \'\^AAA=\'/ -e \'\^XXXXX\ AAA=\'/; my @command2 = ("ssh us@pp -o BatchMode=yes grep $regexpaaa $dir/$file +"); ...
But this time, it doesn't work. ssh command or perl aren't able to interpret correctly the second regexp. The error is: "grep: AAA=: No such file or directory"
Probably exists some perl module that would help me, but I can't use them. Sure I can't.
Some idea?

Thank you very very much.
Hveneticus.

Replies are listed 'Best First'.
Re: perl, ssh and grep
by salva (Canon) on Jun 09, 2017 at 08:02 UTC
    Quoting by hand is hard, let perl do it for you!
    use Net::OpenSSH; my $ssh = Net::OpenSSH->new('us@pp'); my @out = $ssh->capture('grep', '-i', '-e', '^AAA=', '-e', '^XXXXX AAA +=', "$dir/$file");
Re: perl, ssh and grep
by marinersk (Priest) on Jun 08, 2017 at 23:29 UTC

    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.

Re: perl, ssh and grep
by thanos1983 (Parson) on Jun 08, 2017 at 22:29 UTC
Re: perl, ssh and grep
by LanX (Saint) on Jun 08, 2017 at 21:41 UTC
    Welcome to the monastery.

    Unfortunately there is not much Perl in this question...

    Please try some basic debugging and print the content of your command and try if it works without Perl.

    I think you're messing up the shell / grep call.

    > Probably exists some perl module that would help me, but I can't use them. Sure I can't.

    So you want a non Perl solution?

    Not sure if you should ask here then.

    Cheers Rolf
    (addicted to the Perl Programming Language and ☆☆☆☆ :)
    Je suis Charlie!

Re: perl, ssh and grep
by hveneticus (Novice) on Dec 15, 2017 at 15:55 UTC
    Yeah. The right way is to let perl doing the quoting...
    Thanks to everyone