【发布时间】:2020-08-03 07:00:18
【问题描述】:
我正在尝试在远程系统的目录中运行查找命令。 Fabric 有时会更改目录,但有时会失败,具体取决于路径是否包含括号或空格以及我是否使用 shlex.quote() 。处理这个问题的正确方法是什么?
我的代码基本上是这样的:
from shlex import quote
from fabric import Connection
with Connection(remote_login) as c:
with c.cd(quote(node.src)): # Condition 1
# with c.cd(node.src): # Condition 2
result = c.run(r"nice find -maxdepth 1 -type f -printf '%f\n'", echo=True)
如果我使用条件 1,当路径包含括号时它会成功。在这种情况下,Fabric 会生成这一行:
# Fabric output success with parens in path
cd '/data/PixelSizeTestRuby105mm(Zoom248.5mm)' && nice find -maxdepth 1 -type f -printf '%f\n'
但是当路径包含空格时它会失败,因为空格被转义但路径也被引用,而不仅仅是一个或另一个。
# Fabric output failure for spaces in path
cd '/data/Crystal\ Bending\ Test/Bending0' && nice find -maxdepth 1 -type f -printf '%f\n'
sh: line 0: cd: /data/Crystal\ Bending\ Test/Bending0: No such file or directory
如果我改为使用条件 2,则第一个路径失败,第二个路径成功。
# Fabric output failure for parens in path
cd /data/PixelSizeTestRuby105mm(Zoom248.5mm) && nice find -maxdepth 1 -type f -printf '%f\n'
sh: -c: line 0: syntax error near unexpected token `('
sh: -c: line 0: `cd /data/PixelSizeTestRuby105mm(Zoom248.5mm) && nice find -maxdepth 1 -type f -printf '%f\n''
【问题讨论】:
-
你引用的太多了,要么加单引号,要么加反斜杠,但不能同时加。我隐约猜测这是 Fabric 中的一个错误,但我对此并不熟悉。
-
无论如何你不必
cd;只需将find的路径作为第一个参数传递,在任何谓词之前。 -
@tripleee 澄清一下,那些带有 cd 命令的例子是 fabric 的输出;它正在形成导致失败的那些命令。我已经编辑了这个问题,试图让这个问题更清楚。我也想知道是不是bug。我想让 cd 上下文处理工作,因为我需要运行其他命令,如 md5sum,如果不先 cd 到目录,我找不到运行的方法。
-
在将路径传递给
cd时,您不需要引用 anything。所有的引用都应该纯粹在连接的会话实现内部发生。如果常规(即未引用的)路径字符串失败,则该实现中存在错误。 Fabric/Invoke 似乎没有正确引用括号。