简体   繁体   English

从 bash 脚本作为数组访问 python 脚本返回值

[英]Accessing python script return value from bash script as an array

I have a bash command that is as below:我有一个 bash 命令如下:

($(echo my-profiles --my-profiles pytest))

The above bash command returns an array from the python script as below:上面的 bash 命令从 python 脚本返回一个数组,如下所示:

['mock-alchemy', 'pytest-mock', 'pytest-datafixtures', 'pytest-describe', 'pytest-unordered', 'requests-mock']

From the answer of this example, I am accessing the return values as below:这个例子的答案中,我访问的返回值如下:

dependencies=($(echo /path/to/my-script.py --my-profiles pytest}))

When I access the return values from dependencies , I get the following results:当我从dependencies访问返回值时,我得到以下结果:

> echo ${dependencies[0]}
my-profiles

How can I get 'mock-alchemy' instead of the above result?我怎样才能得到'mock-alchemy'而不是上述结果?

My python script is as follows:我的 python 脚本如下:

my-script.py
def main():
    parser = argparse.ArgumentParser(description='My script')
    parser.add_argument('--tox-profiles', dest="profiles", 
                        type=str,
                        default='')
    parsed_args = parser.parse_args()
    dependencies = get_dependencies(args.profiles)

def get_dependencies(profiles):
    return ['mock-alchemy', 'pytest-mock', 'pytest-datafixtures', 'pytest-describe', 'pytest-unordered', 'requests-mock']

($(echo my-profiles --my-profiles pytest))

(... ) execute a subshell. (... )执行一个子shell。 Then $(... ) executes echo .然后$(... )执行echo Command echo outputs my-profiles --my-profiles pytest .命令echo输出my-profiles --my-profiles pytest Then the result of $(...) undergoes word splitting expansion and my-profiles --my-profiles pytest is split into 3 words my-profiles --my-profiles pytest .然后$(...)的结果进行分词扩展my-profiles --my-profiles pytest被分成三个词my-profiles --my-profiles pytest Then the result of splitting is like "rescanned" and becomes a new command to execute, so my-profiles is executed.然后拆分的结果就像“重新扫描”一样,变成了新的命令来执行,所以执行了my-profiles Which actually outputs the output of your program.它实际上输出程序的 output 。

This is all convoluted.这一切都令人费解。 Just run my-profiles --my-profiles pytest , what is echo doing there?只需运行my-profiles --my-profiles pytestecho在那里做什么?

 dependencies=($(echo my-profiles --my-profiles pytest))

dependencies=(... ) is an array assignment. dependencies=(... )是一个数组赋值。 First, $(...) is replaced by the output of command inside.首先将$(...)替换为里面命令的output。 echo my-profiles --my-profiles pytest outputs my-profiles --my-profiles pytest . echo my-profiles --my-profiles pytest输出my-profiles --my-profiles pytest Then the result my-profiles --my-profiles pytest is word split, and becomes 3 words, which are assigned each to a separate array element.然后结果my-profiles --my-profiles pytest是单词拆分,变成 3 个单词,每个单词分配给一个单独的数组元素。

How can I get 'mock-alchemy' instead of the above result?我怎样才能得到“模拟炼金术”而不是上述结果?

You should output the data from your tool in machine-readable format if you intent to use them by machines.如果您打算在机器上使用它们,您应该以机器可读格式 output 工具中的数据。 Because they are quite easy, you could output for example space separated data.因为它们很容易,你可以 output 例如空格分隔的数据。

def get_dependencies(profiles):
    return ' '.join([
       'mock-alchemy', 'pytest-mock', 'pytest-datafixtures', 'pytest-describe', 'pytest-unordered', 'requests-mock'
       ])

# actually print it too!
print(get_dependencies(None))

Then you could read space separated values in bash:然后您可以读取 bash 中的空格分隔值:

output=$(my-profiles --my-profiles pytest)
IFS=' ' read -r -a arr <<<"$output"
declare -p arr
echo "${arr[0]}"

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM