我正在尝试在远程服务器上运行一些命令。我需要在那里获取一些 bash 文件。不幸的是,fabric 似乎(突然,最近?)开始使用 /bin/sh
,但由于我在我的脚本中使用了 bash 语法,所以它中断了。我找到了 this :
If shell is True (the default), run will execute the given command string via a shell interpreter, the value of which may be controlled by setting env.shell (defaulting to something similar to /bin/bash -l -c "".) Any double-quote (") or dollar-sign ($) characters in command will be automatically escaped when shell is True.
我没有改过env.shell,所以不知道fabric为什么开始使用sh
。无论如何,我正在覆盖它,但仍然没有运气:
env.shell = "/bin/bash"
这可能是什么原因造成的?我可以做些什么来强制 fabric 使用bash
?
最佳答案
这需要一些挖掘,但这是我发现的:
可以看到 fabric 在幕后做了什么:
from fabric.api import output
FAB_SHOW_RUNNING = True # Show the command that fabric runs
FAB_SHOW_STDOUT = False # Show the stdout of the command run
FAB_SHOW_STDERR = False # Show the stderr of the command run
FAB_SHOW_DEBUG = True # Increase logging detail for messages
FAB_SHOW_USER = True
FAB_SHOW_STATUS = False # Prevent fabric from using print in some situations (at least in disconnect_all)
FAB_SHOW_WARNINGS = False # Avoid fabric from showing messages about failed commands
output['running'] = FAB_SHOW_RUNNING
output['stdout'] = FAB_SHOW_STDOUT
output['stderr'] = FAB_SHOW_STDERR
output['debug'] = FAB_SHOW_DEBUG
output['user'] = FAB_SHOW_USER
output['status'] = FAB_SHOW_STATUS
output['warnings'] = FAB_SHOW_WARNINGS
事实证明它不是使用 /bin/sh
的结构,但是,因为我(在这种特殊情况下)运行本地命令,所以罪魁祸首是 subprocess模块。为了指定要使用的shell,需要指定shell=True
和executable='/bin/bash'
关于python - 为什么织物使用/bin/sh,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16950435/