使用 Python 的函数返回值为 shell 变量赋值 [英] assigning value to shell variable using a function return value from Python

查看:85
本文介绍了使用 Python 的函数返回值为 shell 变量赋值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个 Python 函数 fooPy() 可以返回一些值.( int/double 或 string)

I have a Python function, fooPy() that returns some value. ( int / double or string)

我想使用这个值并在一个 shell 脚本中分配它.例如下面是python函数:

I want to use this value and assign it in a shell script. For example following is the python function:

def fooPy():
    return "some string" 
    #return 10  .. alternatively, it can be an int

fooPy()

在 shell 脚本中,我尝试了以下操作,但都不起作用.

In the shell script I tried the following things but none of them work.

fooShell = python fooPy.py
#fooShell = $(python fooPy.py)
#fooShell = echo "$(python fooPy.py)"

推荐答案

您可以在 Python 中打印您的值,如下所示:

You can print your value in Python, like this:

print fooPy()

并在您的 shell 脚本中:

and in your shell script:

fooShell=$(python fooPy.py)

确保不要在 shell 脚本中的 = 周围留下空格.

Be sure not to leave spaces around the = in the shell script.

这篇关于使用 Python 的函数返回值为 shell 变量赋值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆