Xonsh 和 rsync 命令

huangapple go评论89阅读模式
英文:

Xonsh and rsync command

问题

我有一个类似这样的测试文件夹:

[20/01/3|2:08:12][samuel@localhost:/tmp]
>>> ls test1
1.txt  2.txt  3.txt  4.txt  5.txt

在正常的 bash/zsh shell 中,这是该命令的输出:

>>> rsync -avz --exclude="2.txt" --dry-run test1/ test2/
sending incremental file list
./
1.txt
3.txt
4.txt
5.txt

sent 138 bytes  received 31 bytes  338.00 bytes/sec
total size is 0  speedup is 0.00 (DRY RUN)
[20/01/3|2:10:42][samuel@localhost:/tmp]

但在 xonsh shell 中,这是输出:

samuel@localhost /tmp $ rsync -avz --exclude="2.txt" --dry-run test1/ test2/
sending incremental file list
./
1.txt
2.txt
3.txt
4.txt
5.txt

sent 156 bytes  received 34 bytes  380.00 bytes/sec
total size is 0  speedup is 0.00 (DRY RUN)
samuel@localhost /tmp $

我尝试过使用单引号,但结果是相同的。

有人能解释一下这个简单的命令出了什么问题吗?

我的 xonsh 版本是 0.9.11,但我也测试过 0.9.13

英文:

I have a test folder like this:

[20/01/3|2:08:12][samuel@localhost:/tmp]
>>> ls test1
1.txt  2.txt  3.txt  4.txt  5.txt

in a normal bash/zsh shell this is the output of the command

>>> rsync -avz --exclude="2.txt" --dry-run test1/ test2/
sending incremental file list
./
1.txt
3.txt
4.txt
5.txt

sent 138 bytes  received 31 bytes  338.00 bytes/sec
total size is 0  speedup is 0.00 (DRY RUN)
[20/01/3|2:10:42][samuel@localhost:/tmp]

but in a xonsh shell, this is the output

samuel@localhost /tmp $ rsync -avz --exclude="2.txt" --dry-run test1/ test2/                                                            
sending incremental file list
./
1.txt
2.txt
3.txt
4.txt
5.txt

sent 156 bytes  received 34 bytes  380.00 bytes/sec
total size is 0  speedup is 0.00 (DRY RUN)
samuel@localhost /tmp $

I try changing also with single apex, but result is the same.

Can anyone explain me where this simple command wrongs ??

My xonsh version is 0.9.11, but I test also with 0.9.13

答案1

得分: 4

当您在xonsh下运行时,不存在类似于shell引用的处理。参数按原样传递给rsync程序。特别是'--exclude="2.txt"'将带着引号传递给rsync。

要排除2.txt而不是"2.txt",在xonsh中的命令应为:

rsync -avz --exclude=2.txt --dry-run test1/ test2/
英文:

When you run under xonsh, there is no processing similar to shell quoting. The parameters are passed AS-IS to the rsync program. In particular, '--exclude="2.txt"' will be passed to rsync with the quotes.

To exclude the 2.txt, and not "2.txt", the command to 'xonsh' should be:

rsync -avz --exclude=2.txt --dry-run test1/ test2/

答案2

得分: 2

Short answers:

  • 删除等号,即 --exclude "2.txt"
  • 或者删除引号,即 --exclude=2.txt
  • 或者使用 Python 替代,即 --exclude=@("2.txt")
  • 或者在不更改命令的情况下运行宏调用,即 bash -c! find ...
英文:

Short answers:

  • remove equal sign i.e. --exclude "2.txt"
  • or remove quotes i.e. --exclude=2.txt
  • or use python substitution i.e. --exclude=@("2.txt")
  • or run macro call without any changes of the command i.e. bash -c! find ...

Detailed answer:

To understand the difference try to trace the command in bash and in xonsh.

Bash:

set -x
rsync -avz --exclude="2.txt" --dry-run test1/ test2/
# + rsync -avz --exclude=2.txt --dry-run test1/ test2/

Xonsh:

$XONSH_TRACE_SUBPROC=True
rsync -avz --exclude="2.txt" --dry-run test1/ test2/
# (['rsync', '-avz', '--exclude="2.txt"', '--dry-run', 'test1/', 'test2/'],)

Here you can see the difference: bash removed quotes for the argument because quotes have special meaning in bash but xonsh leave them in the argument.

To make it works you can remove quotes OR pass the filename using python substitution in xonsh:

rsync -avz --exclude=2.txt --dry-run test1/ test2/
rsync -avz --exclude=@("2.txt") --dry-run test1/ test2/

Another way is to use the xonsh macro command and just run bash line as is:

bash -c! rsync -avz --exclude="2.txt" --dry-run test1/ test2/

To simplify this approach there is xontrib-sh extension that allows just add the ! sign before the command:

! rsync -avz --exclude="2.txt" --dry-run test1/ test2/

Also:

huangapple
  • 本文由 发表于 2020年1月3日 21:17:38
  • 转载请务必保留本文链接:https://go.coder-hub.com/59579274.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定