Say you want to copy file foo(s)
using scp
.
As shown below, scp
treats the source and target as shell literals, so you pass the following arguments to scp
:
scp
-p
--
host1.com:foo\(s\)
orhost1.com:'foo(s)'
host2.com:foo\(s\)
orhost2.com:'foo(s)'
You can do that using the multi-argument syntax of system
plus an escaping function.
use String::ShellQuote qw( shell_quote );
my $source = $from_server . ":" . shell_quote("$from_path/$filename");
my $target = $to_server . ":" . shell_quote("$to_path/$filename");
system('scp', '-p', '--', $source, $target);
If you really wanted to build a shell command, use shell_quote
as usual.
my $cmd = shell_quote('scp', '-p', '--', $source, $target);
$ ssh ikegami@host.com 'mkdir foo ; touch foo/a foo/b foo/"*" ; ls -1 foo'
*
a
b
$ mkdir foo ; ls -1 foo
$ scp 'ikegami@host.com:foo/*' foo
* 100% 0 0.0KB/s 00:00
a 100% 0 0.0KB/s 00:00
b 100% 0 0.0KB/s 00:00
$ ls -1 foo
*
a
b
$ rm foo/* ; ls -1 foo
$ scp 'ikegami@host.com:foo/\*' foo
* 100% 0 0.0KB/s 00:00
$ ls -1 foo
*