通过 os.system 将变量从 python 传递到 bash shell 脚本
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/13944076/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
passing variables from python to bash shell script via os.system
提问by user1815498
In the following code, I construct a variable $probe1 that I want to then pass to a bash script. I the toy example below, the output is blank, i.e. $probe1 is not recognized the the bash shell script within the os.system call. What needs to be done?
在以下代码中,我构造了一个变量 $probe1,然后我想将其传递给 bash 脚本。我在下面的玩具示例中,输出为空白,即 $probe1 无法识别 os.system 调用中的 bash shell 脚本。需要做什么?
for line1 in datfile:
datmat=datmat+[line1.rstrip('\n').split('\t')]
probe=datmat[i][0]
snp1=datmat[i][2]
probe1='permprobes'+probe+'pheno.pphe'
os.system('echo $probe1')
回答by satoru
Seems like this is what you are trying to do:
似乎这就是您要执行的操作:
In [2]: os.environ['probe1'] = 'hello'
In [3]: os.system('echo $probe1')
hello
But I have no idea why you would like to do this ...
但我不知道你为什么要这样做......
回答by jgritty
os.system('echo {0}'.format(probe1))
probe1 is a python variable, not a shell variable.
probe1 是一个 python 变量,而不是一个 shell 变量。
os.environ['probe1'] = probe1
will set the bash environment variable to the python variable contents. Once the python script exits though, the environment variable goes away.
将 bash 环境变量设置为 python 变量内容。一旦 python 脚本退出,环境变量就会消失。

