我正在尝试在远程系统的目录中运行 find 命令。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''