1

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.

1 Answer 1

2
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.

Sign up to request clarification or add additional context in comments.

3 Comments

yes thank you for your reply. Now, it is not giving me an error but it is unable to get the matched patterns. That is, I am getting a "find:no match" when I store the same pattern in a variable but is running when I use value of pattern directly in command. Thanks again :)
my $pattern = "40786"; #This runs when I write my @files = $ssh->capture2(qq(find /u1/temp -name "40786") ); #but returns not found when I use variable $pattern Sorry if its a silly question. I am new to perl and couldn't find an explanation elsewhere.
@emma That should work. Without the surrounding code, I can't tell you what's wrong. I'd suggest you ask a new question and provide a complete program that doesn't work.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.