从在bash Python函数收集返回值 [英] Collecting return values from python functions in bash

查看:199
本文介绍了从在bash Python函数收集返回值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我实现一个bash脚本,将调用一个python脚本的功能/方法。我想收集这些函数的返回valuie到调用bash脚本的局部变量。

I am implementing a bash script that will call a python script's function/method. I want to collect the return valuie of this function into a local variable in the calling bash script.

try1.sh包括:

try1.sh contains:

#!/bin/sh
RETURN_VALUE=`python -c 'import try3; try3.printTry()'`
echo $RETURN_VALUE

现在的python脚本:

Now the python script:

#!/usr/bin/python

def printTry():
    print 'Hello World'
    return 'true'

在excuting的bash脚本:

on excuting the bash script:

$./tr1.sh
Hello World

有没有真还是在那个地方作为需要任何其他类型的呼应到标准输出。

there is no 'true' or in that place any other type echoed to stdout as is desired.

另一件事我会希望能够做的是,我avtual蟒蛇code将有20-30左右返回功能,我的软件状态机的各种状态值,我会从一个bash脚本调用这些函数。在bash脚本,我要这些返回值存储在本地变量,要进一步用倒在调用bash脚本实现的状态机逻辑。

Another thing I would want to be able to do is, my avtual python code will have around 20-30 functions returning various state values of my software state machine, and I would call these functions from a bash script. In the bash script, I have to store these return values in local variables which are to be used further down the state machine logic implemented in the calling bash script.

对于每一个值,我会做蟒蛇-c'进口python_module; python_module.method_name',这将一次又一次地重新枚举状态机的定义的状态,这是我不想要的。我想避免使整个python脚本运行只是调用一个单一的功能。这可能吗?

For each value, I would have do the python -c 'import python_module; python_module.method_name', which would re-enumerate the defined states of the state machine again and again, which I do not want. I want to avoid making the entire python script run just for calling a single function. Is that possible?

可以认为这里有什么可能的解决方案/建议/想法?

What possible solutions/suggestions/ideas can be thought of here?

我想AP preciate的答复。

I would appreciate the replies.

要澄清我的意图,我们的任务是必须通过改善可读性python脚本取代了bash脚本的一部分。在bash脚本真的是非常大(15000〜线),因此不能由一个Python脚本完全取代。可idetified加以改进,使零件可通过蟒代替。

To clarify my intent, the task is to have a part of the bash script replaced by the python script for improving readability. The bash script is really very large(~ 15000 lines), and hence cannot be replaced by a single python script entirely. So parts which can be idetified to be improved can be replaced by python.

另外,我还以为由python脚本替换整个bash脚本由维克多在下面的评论暗示,但它不会在我的情况是可行的。因此,我就必须有国家机器分为bash和蟒蛇,蟒蛇的地方会有些方法需要返回由bash脚本所需的状态值。

Also, I had thought of replacing the entire bash script by a python script as suggested by Victor in the comment below, but it wouldn't be feasible in my situation. Hence, I would have to have the state machine divided into bash and python, where python would have some required methods returning state values required by the bash script.

问候,
优素福Husainy。

Regards, Yusuf Husainy.

推荐答案

如果你不关心什么Python函数打印到标准输出,你可以这样做:

If you don't care about what the python function prints to stdout, you could do this:

$ py_ret_val=$(python -c '
from __future__ import print_function
import sys, try3
print(try3.printTry(), file=sys.stderr)
' 2>&1 1>/dev/null)

$ echo $py_ret_val
true

这篇关于从在bash Python函数收集返回值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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