2015-12-08 166 views
1

有人可以帮助我了解如何从shell脚本中调用'x'函数的语法。这里的挑战是,从shell脚本需要动态地传递a,b。从shell脚本中调用python函数

你的建议非常感谢。

Python代码:

def x(a,b): 
    '''x(a,b) --> [a,b]''' 
    return [a,b] 

def add(a,b): 
    '''add(a,b) --> a+b''' 
    return a+b 

def test(): 
    '''test code for all modules''' 
    print "testing x..." 
    print x(1,2) 
    print x('a','b') 
    print x([1,2,3],[4,5,6]) 
    print "testing add..." 
    print add(1,2) 
    print add('a','b') 
    print add([1,2,3],[4,5,6]) 
    return 
+0

哇。在发布失败之前进行最终搜索。 –

+0

@MadPhysicist,但答案是旧的,而不是Python3兼容 –

+0

@JohnLaRooy这不是一个Python3的问题,* nix上的'$ python'应该总是运行python2。如果您想更新答案,请对副本进行更新。 –

回答

2

如果你保存文件foo.py,您可以从shell

python -c "import foo; print(foo.x(1, 2))" 

结果运行此可以从标准输出读取。

+0

感谢您的回应..但我需要动态传递参数。像python -c“import foo; print(foo.x($ {var1},$ {var2}))” – SVN

+0

所以那样做。什么阻止你? –

+0

这不适用于我们的红帽企业Linux服务器版本5.11,请帮助我确定替代方案 – SVN