Search code examples
regexperlshellremote-accessopenssh

Using local perl variables in shell command


I am trying to run a remote find command but I have to pass it local variables whose value I get from command line arguments from user. But, I am getting an "undefined variable" error. I am doing something like :

my $ssh = Net::OpenSSH->new($host);
#I am getting a part of the $path and $pattern from command line
#I have to look for $pattern at the $path
my @files = $ssh->capture(q(find $path -name $pattern) );

This command runs fine when I give the exact path and pattern but I get error when I replace it with variables.


Solution

  • q(find $path -name $pattern)
    

    This is the literal string find $path -name $pattern, dollar signs and all.

    Perl has several different kinds of quotes. "" will interpolate variables (ie. replace them with their values), '' will not. q() is another way to write ''. You want qq().

    See Quote and Quote-Like Operators in perlop for more info.