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

Hi, How do I do this in perl. ssh apple1 'for i in `ls /home/dist/` ; do cat $i ; done ' I am getting an error when I do this html @data=`ssh $key 'for i in system"ls /home/dist/"; do cat $i ; done '`; print "@data"; /html

Replies are listed 'Best First'.
Re: ssh system command
by ikegami (Patriarch) on Sep 11, 2007 at 20:41 UTC

    I'm sure ` means something totally different than " to the remote shell. The $ in $i also needs to be escaped (or else Perl will try to interpolate its variable).

    @data = `ssh $key 'for i in \`ls /home/dist/\` ; do cat \$i ; done'`;

    or

    @data = qx!ssh $key 'for i in `ls /home/dist/` ; do cat \$i ; done'!;

    However, isn't
    for i in `ls /home/dist/` ; do cat $i ; done
    the same as
    cat /home/dist/*?
    If so, the following would be simpler.

    @data = `ssh $key 'cat ls /home/dist/*'`;
      AH thats nice ...thank you very much for your help that takes care of the problem. thank u
Re: ssh system command
by salva (Canon) on Sep 11, 2007 at 21:54 UTC
    or using one of the SFTP client modules:
    use Net::SFTP::Foreign; my $s = Net::SFTP::Foreign->new('apple1'); die "sftp connection failed: ". $s->error if $s->error; my @fn = $sftp->glob('/home/dist/*', names_only => 1); for my $fn (@fn) { my $data = $s->get_content($fn); if ($s->error) { warn "unable to read $fn: ".$s->error } else { print $data; } }
    At first, it may seem more complex than a shell based solution, but if you need to change the functionality of your script later, or check for errors or anything else, having a full file system API at your disposal would help