Linux 如何为rsync创建别名?

Linux 如何为rsync创建别名?,linux,rsync,scp,Linux,Rsync,Scp,我经常使用这个命令来同步远程和本地 rsync -xyz --foo=bar some_files user@remote.com:remote_dir 所以我想用一个别名来简化它,如下所示: up_remote () { local_files=${@:1:$((${#}-1))} # treat argument[1:last-1] as local files remote_dir="${@[$#]}" # treat argument[last] as remote_

我经常使用这个命令来同步远程和本地

rsync -xyz --foo=bar some_files user@remote.com:remote_dir
所以我想用一个别名来简化它,如下所示:

up_remote () {
    local_files=${@:1:$((${#}-1))}  # treat argument[1:last-1] as local files
    remote_dir="${@[$#]}" # treat argument[last] as remote_dir
    echo $local_files
    echo $remote_dir
    rsync -xyz --foo=bar "$local_files" user@remote.com:"$remote_dir"
}
rsync -xyz --foo=bar 'local1 local2' user@remote.com:"$remote_dir"
但如果我通过三个或更多的论点,它将不起作用:

up_remote local1 local2 remote_dir
当我用
set-x
调试此函数时,我发现该函数将生成
rsync
如下所示:

up_remote () {
    local_files=${@:1:$((${#}-1))}  # treat argument[1:last-1] as local files
    remote_dir="${@[$#]}" # treat argument[last] as remote_dir
    echo $local_files
    echo $remote_dir
    rsync -xyz --foo=bar "$local_files" user@remote.com:"$remote_dir"
}
rsync -xyz --foo=bar 'local1 local2' user@remote.com:"$remote_dir"
请注意
local1 local2
周围的单引号(
)。如果我删除这些单引号,
rsync
将正常工作,但我不知道如何做到这一点


欢迎提供任何建议。

这不是一个真正的答案,但我用perl实现了这一点,它可以工作:

#!/usr/bin/perl

use strict;
use warnings;

my @args = @ARGV;
my $files;

my $destination=pop(@args);

foreach(@args){
    $files.="'".$_."' ";
}

system("rsync -xyz --foo=bar $files user\@remote.com:$destination");

您可以在路径中复制此脚本或为其创建别名。

您只需删除
$local\u files
周围的双引号即可:

up_remote () {
  local_files=${@:1:$((${#}-1))}
  remote_dir="${@:$#}"
  rsync -xyz --foo=bar $local_files a.b.com:"$remote_dir"
}

注意:我还更改了选择
remote\u dir
的方式,无法在我的bash版本中工作。

如果文件名包含空格,则此操作将失败。为什么不使用字符串::ShellQuote这样的东西呢?对于空白,您是对的,一个解决方法是在foreach中添加单引号(在我的回答中编辑),现在如果文件名有单引号,它就会中断。目的地也一样。正如我所说,有一些模块可以为您进行转义抱歉,但我不知道如何使用bash正确地完成转义。祝你好运;)当然,这是一个解决方案,而且会起作用。您是否尝试在
本地_文件中传递两个或多个参数?如下所示:
up\u remote file1 file2/tmp
up\u remote file*/tmp
是的,这两种调用样式都工作正常。你使用的是什么版本的bash,我的函数有什么输出?你是对的。我以前在
zsh
中使用过,这是我的错误。它在我的
bash
中工作。谢谢