将python数组传递给bash脚本(并将bash变量传递给python函数)

时间:2022-04-02 11:20:31

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.

我编写了一个Python模块,其中包含返回数组的函数。我希望能够访问从python模块返回的字符串数组,并在bash脚本中迭代,所以我可以迭代数组元素。

For example:

Python module (mymod)

def foo():
    return ('String', 'Tuple', 'From', 'Python' )

def foo1(numargs):
    return [x for x in range(numargs)]

Bash script

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

How can I implement this in bash?.

我怎样才能在bash中实现这个?

version Info:

Python 2.6.5 bash: 4.1.5

Python 2.6.5 bash:4.1.5

4 个解决方案

#1


13  

Second try - this time shell takes the integration brunt.

第二次尝试 - 这次shell采取整合首当其冲。

Given foo.py containing this:

鉴于foo.py包含:

def foo():
        foo = ('String', 'Tuple', 'From', 'Python' )
        return foo

Then write your bash script as follows:

然后按如下方式编写bash脚本:

#!/bin/bash
FOO=`python -c 'from foo import *; print " ".join(foo())'`
for x in $FOO:
do
        echo "This is foo.sh: $x"
done

The remainder is first answer that drives integration from the Python end.

其余部分是第一个从Python端推动集成的答案。

Python

import os
import subprocess

foo = ('String', 'Tuple', 'From', 'Python' )

os.putenv('FOO', ' '.join(foo))

subprocess.call('./foo.sh')

bash

#!/bin/bash
for x in $FOO
do
        echo "This is foo.sh: $x"
done

#2


1  

In addition, you can tell python process to read STDIN with "-" as in

另外,你可以告诉python进程用“ - ”读取STDIN,如

echo "print 'test'" | python -

Now you can define multiline snippets of python code and pass them into subshell

现在您可以定义python代码的多行代码片段并将它们传递给子shell

FOO=$( python - <<PYTHON

def foo():
    return ('String', 'Tuple', 'From', 'Python')

print ' '.join(foo())

PYTHON
)

for x in $FOO
do
    echo "$x"
done

You can also use env and set to list/pass environment and local variables from bash to python (into ".." strings).

您还可以使用env和set来列出/传递从bash到python的环境和局部变量(到“..”字符串)。

#3


0  

In lieu of something like object serialization, perhaps one way is to print a list of comma separated values and pipe them from the command line.

代替对象序列化之类的东西,也许一种方法是打印逗号分隔值列表并从命令行管道它们。

Then you can do something like:

然后你可以这样做:

> python script.py | sh shellscript.sh

#4


0  

This helps too. script.py:

这也有帮助。 script.py:

 a = ['String','Tuple','From','Python']

    for i in range(len(a)):

            print(a[i])

and then we make the following bash script pyth.sh

然后我们制作以下bash脚本pyth.sh

#!/bin/bash

python script.py > tempfile.txt
readarray a < tempfile.txt
rm tempfile.txt

for j in "${a[@]}"
do 
      echo $j
done

sh pyth.sh

#1


13  

Second try - this time shell takes the integration brunt.

第二次尝试 - 这次shell采取整合首当其冲。

Given foo.py containing this:

鉴于foo.py包含:

def foo():
        foo = ('String', 'Tuple', 'From', 'Python' )
        return foo

Then write your bash script as follows:

然后按如下方式编写bash脚本:

#!/bin/bash
FOO=`python -c 'from foo import *; print " ".join(foo())'`
for x in $FOO:
do
        echo "This is foo.sh: $x"
done

The remainder is first answer that drives integration from the Python end.

其余部分是第一个从Python端推动集成的答案。

Python

import os
import subprocess

foo = ('String', 'Tuple', 'From', 'Python' )

os.putenv('FOO', ' '.join(foo))

subprocess.call('./foo.sh')

bash

#!/bin/bash
for x in $FOO
do
        echo "This is foo.sh: $x"
done

#2


1  

In addition, you can tell python process to read STDIN with "-" as in

另外,你可以告诉python进程用“ - ”读取STDIN,如

echo "print 'test'" | python -

Now you can define multiline snippets of python code and pass them into subshell

现在您可以定义python代码的多行代码片段并将它们传递给子shell

FOO=$( python - <<PYTHON

def foo():
    return ('String', 'Tuple', 'From', 'Python')

print ' '.join(foo())

PYTHON
)

for x in $FOO
do
    echo "$x"
done

You can also use env and set to list/pass environment and local variables from bash to python (into ".." strings).

您还可以使用env和set来列出/传递从bash到python的环境和局部变量(到“..”字符串)。

#3


0  

In lieu of something like object serialization, perhaps one way is to print a list of comma separated values and pipe them from the command line.

代替对象序列化之类的东西,也许一种方法是打印逗号分隔值列表并从命令行管道它们。

Then you can do something like:

然后你可以这样做:

> python script.py | sh shellscript.sh

#4


0  

This helps too. script.py:

这也有帮助。 script.py:

 a = ['String','Tuple','From','Python']

    for i in range(len(a)):

            print(a[i])

and then we make the following bash script pyth.sh

然后我们制作以下bash脚本pyth.sh

#!/bin/bash

python script.py > tempfile.txt
readarray a < tempfile.txt
rm tempfile.txt

for j in "${a[@]}"
do 
      echo $j
done

sh pyth.sh