You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何在Perl的system调用中向rsync传递含空格的路径变量

解决Perl中system调用rsync传递含空格路径的问题

核心解决方案:使用system的列表调用模式

Perl的system函数有两种调用方式,列表形式会直接将每个元素作为独立的命令行参数传递给程序,跳过shell的分词解析,这是处理含空格/特殊字符路径的最佳方式,无需手动转义空格。

修正后的代码示例

  1. 保持路径变量原样定义(不需要手动添加反斜杠转义空格):
my $mht_user = "username";
my $mht_share = "/mypath/with some space/01. continue/another space/";
my $mht_dstpath = "$mht_user\@$mht_host:$mht_share";
  1. 用列表形式调用rsync:
system(
    '/usr/bin/rsync',
    '-acz', '-H', '--delete',
    '/my source path',
    $mht_dstpath
);

为什么之前的方法失效?

  • 字符串拼接调用system时,shell会将空格视为参数分隔符,即使手动添加反斜杠,Perl字符串中的反斜杠需要额外转义(如\\ ),但仍容易出现解析错误。
  • 列表调用模式直接绕过shell,每个参数被完整传递给rsync,含空格的路径会被识别为单个完整参数,不会被截断。

备选方案(不推荐)

如果因特殊需求必须使用字符串形式,可以借助quotemeta函数或String::ShellQuote模块对路径进行shell安全转义,但列表模式更简洁可靠:

use String::ShellQuote qw(shell_quote);
my $source_quoted = shell_quote('/my source path');
my $dst_quoted = shell_quote($mht_dstpath);
system("/usr/bin/rsync -acz -H --delete $source_quoted $dst_quoted");

内容的提问来源于stack exchange,提问作者Lumos

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.08 01:25:12