传递蟒蛇阵bash脚本(并通过bash的变量蟒蛇功能) [英] Passing python array to bash script (and passing bash variable to python function)
本文介绍了传递蟒蛇阵bash脚本(并通过bash的变量蟒蛇功能)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我已经写它包含返回数组功能的Python模块。我希望能够访问从Python模块返回的字符串数组,并在bash脚本遍历,所以我可以遍历数组元素。
I have written a Python module which contains functions that return arrays. I want to be able to access the string arrays returned from the python module, and iterate over in a bash script, so I may iterate over the array elements.
例如:
def foo():
return ('String', 'Tuple', 'From', 'Python' )
def foo1(numargs):
return [x for x in range(numargs)]
Bash脚本
foo_array = .... # obtain array from mymod.foo()
for i in "${foo_array[@]}"
do
echo $i
done
foo1_array = .... # obtain array from mymod.foo1(pass arg count from bash)
for j in "${foo1_array[@]}"
do
echo $j
done
我怎么能在bash实现这个?
How can I implement this in bash?.
版本信息:
2.6.5的Python
庆典:4.1.5
Python 2.6.5 bash: 4.1.5
推荐答案
第二个尝试 - 这一次的外壳采用一体化首当其冲
Second try - this time shell takes the integration brunt.
由于 foo.py 的包含此:
def foo():
foo = ('String', 'Tuple', 'From', 'Python' )
return foo
然后如下编写bash脚本:
Then write your bash script as follows:
#!/bin/bash
FOO=`python -c 'from foo import *; print " ".join(foo())'`
for x in $FOO:
do
echo "This is foo.sh: $x"
done
的其余部分是从Python最终推动一体化的第一个答案。
The remainder is first answer that drives integration from the Python end.
的的Python 的
import os
import subprocess
foo = ('String', 'Tuple', 'From', 'Python' )
os.putenv('FOO', ' '.join(foo))
subprocess.call('./foo.sh')
的庆典的
#!/bin/bash
for x in $FOO
do
echo "This is foo.sh: $x"
done
这篇关于传递蟒蛇阵bash脚本(并通过bash的变量蟒蛇功能)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文