python - 如何通过Python执行任意shell脚本并传递多个变量?

标签 python ruby macos shell environment-variables

我正在用Python构建一个应用程序插件,它允许用户使用简单的脚本任意扩展应用程序(在Mac OS X下工作)。执行 Python 脚本很容易,但有些用户更喜欢使用 Ruby 等语言。

根据我所读到的内容,我可以使用 subprocess 轻松执行 Ruby 脚本(或其他任意 shell 脚本),并使用管道捕获其输出;这不是问题,网上有很多例子。但是,我需要为脚本提供多个变量(例如一段文本以及有关脚本正在修改的文本的一些简单 bool 信息),并且我无法找出执行此操作的最佳方法。

有人对实现这一目标的最佳方法有建议吗?我的目标是为脚本提供所需的信息,并使用访问脚本中的信息所需的最少代码。

提前致谢!

最佳答案

参见http://docs.python.org/library/subprocess.html#using-the-subprocess-module

args should be a string, or a sequence of program arguments. The program to execute is normally the first item in the args sequence or the string if a string is given, but can be explicitly set by using the executable argument.

因此,您的调用可以如下所示

p = subprocess.Popen( args=["script.sh", "-p", p_opt, "-v", v_opt, arg1, arg2] )

您已将任意 Python 值放入 subprocess.Popen 的参数中。

关于python - 如何通过Python执行任意shell脚本并传递多个变量?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/714360/

相关文章:

ruby-on-rails - mysql2 gem 0.3.15 提供编码设置为 "utf8"的 ASCII-8BIT

macos - Mac中的Kubernetes连接错误

python - 是否可以使用 matplotlib 在 map 上绘制图像?

python - 如何防止 Python 的 urllib(2) 跟随重定向

ruby-on-rails - rails : Export data in google spreadsheet

ruby - 如何使用公钥字符串创建 OpenSSL::PKey?

macos - git checkout -- <files> 不丢弃更改?

objective-c - NSTextField 不可编辑时自动增加宽度

python - 如何在 PySpark 中读取 Avro 文件

python - 将 curl 翻译成 python urllib2