【发布时间】:2016-04-18 23:32:45
【问题描述】:
我可以像终端中的 bash 命令一样轻松获取 bash 脚本(没有 shebang),但尝试通过 python 命令执行相同操作
sourcevars = "cd /etc/openvpn/easy-rsa && . ./vars"
runSourcevars = subprocess.Popen(sourcevars, shell = True)
或
sourcevars = [". /etc/openvpn/easy-rsa/vars"]
runSourcevars = subprocess.Popen(sourcevars, shell = True)
我收到:
请首先获取 vars 脚本(即“source ./vars”) 确保您已对其进行编辑以反映您的配置。
怎么了,如何正确地做到这一点?我在这里阅读了一些主题,例如here,但使用给定的建议无法解决我的问题。请举例说明。
更新:
# os.chdir = ('/etc/openvpn/easy-rsa')
initvars = "cd /etc/openvpn/easy-rsa && . ./vars && ./easy-rsa ..."
# initvars = "cd /etc/openvpn/easy-rsa && . ./vars"
# initvars = [". /etc/openvpn/easy-rsa/vars"]
cleanall = ["/etc/openvpn/easy-rsa/clean-all"]
# buildca = ["printf '\n\n\n\n\n\n\n\n\n' | /etc/openvpn/easy-rsa/build-ca"]
# buildkey = ["printf '\n\n\n\n\n\n\n\n\n\nyes\n ' | /etc/openvpn/easy-rsa/build-key AAAAAA"]
# buildca = "cd /etc/openvpn/easy-rsa && printf '\n\n\n\n\n\n\n\n\n' | ./build-ca"
runInitvars = subprocess.Popen(cmd, shell = True)
# runInitvars = subprocess.Popen(initvars,stdout=subprocess.PIPE, shell = True, executable="/bin/bash")
runCleanall = subprocess.Popen(cleanall , shell=True)
# runBuildca = subprocess.Popen(buildca , shell=True)
# runBuildca.communicate()
# runBuildKey = subprocess.Popen(buildkey, shell=True )
更新 2
buildca = ["printf '\n\n\n\n\n\n\n\n\n' | /etc/openvpn/easy-rsa/build-ca"]
runcommands = subprocess.Popen(initvars+cleanall+buildca, shell = True)
【问题讨论】:
-
您从那些 sn-ps 或之后的某些 other 调用中得到了那个错误?因为这些应该可以正常工作,但只会对该 一个 shell 实例有效,不会影响以后的 shell 实例或正在运行的 python 脚本/环境。
-
通常我需要运行一组命令,其中显示的是第一个命令并从一开始就出现此错误
-
您只需要在一个
subprocess.Popen实例中运行所有命令,因为它们的范围仅限于一个 shell。 -
顺便说一句,在赋值中应该在
=s 周围使用空格,但不能在参数列表中使用空格。见python.org/dev/peps/pep-0008/#other-recommendations -
您显示的示例(并感谢您对其进行编辑)没有按照我的建议执行,尽管有相反的说法:它仍在单独运行您的
cleanall命令运行initvars的 shell。