一、python脚本

#!/usr/bin/env python
#-*- coding:utf-8 -*-
#scriptname:test.py

def get_foo():
    return "foo"

def get_bar():
    return "bar"

二、调用

1、通过shell调用里面的get_foo,只需要在shell中执行一个调用的命令行即可

python -c 'import test; print test.get_foo()' 

备注:-c 选项只是告诉python来执行一些python命令。

2、将结果存储在变量中

RESULT_FOO=`python -c 'import test; print test.get_foo()'` 

或者,等效于:

RESULT=$(python -c 'import test; print test.get_foo()')  

3、一次调用所有方法,放入一个集合中,再调用切割方法获取相应的值

ALL_RESULTS=$(python -c 'import test; print test.get_foo(), test.get_bar()')  

如果需要第二个结果,并将其放入RESULT_BAR:

RESULT_BAR=$(echo $ALL_RESULTS | cut -d' ' -f2)  

shell调用python函数

相关文章:

  • 2022-02-11
  • 2021-12-21
  • 2021-11-07
  • 2022-01-21
  • 2022-12-23
  • 2021-11-20
猜你喜欢
  • 2022-03-05
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2021-07-17
相关资源
相似解决方案