我应该如何在 Python Fabric 中使用 extglob?
我正在尝试使用 Fabric (v2.6) 来运行一些利用 bash 的 extglob 和 dotglob 的命令。
当我运行时:
c.run(f"shopt -s extglob dotglob && rm -Rf {project_path}* !(.|..|.venv) && shopt -u extglob dotglob")
我收到此错误:
`bash: -c: line 0: syntax error near unexpected token `('`
我正在使用 &&
因为我发现在单独的运行调用中执行 shopt -s extglob dotglob
不会持续存在随后的运行调用。我非常确定使用 &&
会启用 extglob 和 dotglob,因为当我这样做时:
`c.run("shopt -s extglob dotglob && shopt")`
它会打印出选项列表,并且 extglob 和 dotglob 都已启用。
我这里哪里出错了?
I am trying to use fabric (v2.6) to run some commands that make use of bash's extglob and dotglob.
When I run:
c.run(f"shopt -s extglob dotglob && rm -Rf {project_path}* !(.|..|.venv) && shopt -u extglob dotglob")
I get this error:
`bash: -c: line 0: syntax error near unexpected token `('`
I am using the &&
because I found doing shopt -s extglob dotglob
in a separate run call doesn't persist for the subsequent run calls. I'm pretty sure using &&
is enabling extglob and dotglob because when I do this:
`c.run("shopt -s extglob dotglob && shopt")`
It prints out the list of options and extglob and dotglob are both enabled.
Where am I going wrong here?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
来自 bash wiki:
因此,您必须适当更改 python 代码,以便使用换行符而不是
&&
。或者直接在 python 中执行 bash 调用所做的操作。
From the bash wiki:
So you have to change your python code appropriately so that a newline is used instead of
&&
.Or just do what the bash invocation does directly in python.
不幸的是,extglob 似乎无法与 Python Fabric 一起使用。
来自 bash 文档
但来自 Fabric 文档
幸运的是,使用 Bash 的 GLOBIGNORE shell 变量也可以实现类似的效果
在扩展通配符时,这也可以轻松地忽略
.
和..
,因此要删除目录中的所有文件(除了“.venv”),我们可以这样做It seems
extglob
can't be used with Python Fabric unfortunately.From the bash docs
But from the Fabric docs
Fortunately, a similar thing can be achieved using Bash's GLOBIGNORE shell variable instead
This also handily ignores
.
and..
when expanding wildcards, so to remove all files - except '.venv' - in a directory, we can do