0
$var = "/nfs/sc/disks/darshils/scripts/";

here $var is a string

system 'rsync -av $var/.*pl /scripts/';

system command use for in perl lang I can execute any unix command

Is that way is it possible to use $var in rsync command? or anyother way to use $var in rsync command?

  • 1
    Stack Overflow is a site for programming and development questions. This question appears to be off-topic because it is not about programming or development. See [What topics can I ask about here](http://stackoverflow.com/help/on-topic) in the Help Center. Perhaps [Super User](http://superuser.com/) or [Unix & Linux Stack Exchange](http://unix.stackexchange.com/) would be a better place to ask. – jww Aug 23 '18 at 12:30
  • 2
    @jww the OP is asking a question about Perl, not about `rsync`. It fits nicely here! – salva Aug 23 '18 at 12:49

3 Answers3

3

You can make your system call a little safer.

There are two forms of system. The string form gives system one argument. The shell then interprets the string to figure out what to do. That might be something other than what you intend when you interpolate a variable. What is $var is something like ; rm -rf? That ; ends the command and allows you to start a new one.

The list form is a bit safer. None of the arguments after the command are treated specially by the shell. If there's a shell metacharacter in $var it's just it's literal self in this form:

 system 'rsync', '-av', "$var/.*pl", '/scripts/';

Perl also has "taint-checking" that marks data that has come from outside your program (user input, files, whatever). Tainted data is viral; use it with untainted data and you get more tainted data. When you try to use tainted data to do something outside your program (like a system), you get an error.

Check out perlsec. I also have a chapter on this in Mastering Perl. Good luck!

brian d foy
  • 129,424
  • 31
  • 207
  • 592
1

No, but there is a proper way to use quotes.

In Perl, the string $var is interpolated inside double quotes (and the double quote constructions qq{...}) but treated literally inside single quotes (and q{...}). So you just want to change your system call to

system "rsync -av $var/.*pl /scripts/";
mob
  • 117,087
  • 18
  • 149
  • 283
1

Try to use the Perl File::Rsync module.

brian d foy
  • 129,424
  • 31
  • 207
  • 592