使用shell调用python中的函数
来源:互联网 发布:java进销存软件源码 编辑:程序博客网 时间:2024/06/07 16:09
最近遇到一个需求,需要通过shell调用python中的一个函数,发现其实也挺简单的:
python脚本如下:
test.py:
import ConfigParserconfig = ConfigParser.ConfigParser()config.read("test.conf")def get_foo(): return config.get("locations", "foo")def get_bar(): return config.get("locations", "bar")
我想通过shell调用里面的get_foo,只需要在shell中执行一个调用的命令行即可:
python -c 'import test; print test.get_foo()'
-c选项只是告诉python来执行一些python命令。
为了将结果存储在变量中,你可以因此这样做:
RESULT_FOO=`python -c 'import test; print test.get_foo()'`
或者,等效于:
RESULT=$(python -c 'import test; print test.get_foo()')
我们也可以一次调用所有方法,放入一个集合中,再调用切割方法获取相应的值:
ALL_RESULTS=$(python -c 'import test; print test.get_foo(), test.get_bar()')
如果需要第二个结果,并将其放入RESULT_BAR:
RESULT_BAR=$(echo $ALL_RESULTS | cut -d' ' -f2)
0 0
- 使用shell调用python中的函数
- Shell中的函数调用
- Shell中的函数调用
- Linux Shell 之 Shell中的函数调用
- Linux Shell 之 Shell中的函数调用
- Linux Shell 之 Shell中的函数调用
- Linux Shell 之 Shell中的函数调用
- Linux Shell 之 Shell中的函数调用
- Linux Shell 之 Shell中的函数调用
- Linux Shell 之 Shell中的函数调用
- Linux Shell 之 Shell中的函数调用
- 一个有用函数:python调用shell命令
- Linux shell中的函数调用实践
- 不同shell文件中的函数调用
- shell函数--函数调用
- C语言调用Python脚本中的函数
- 调用其它python脚本中的函数
- python中的setdefaultencoding函数使用
- document.createElement()的用法
- 安卓自定义对话框及The specified child already has a child问题
- Ubuntu 14.04 Eclipse for Android 集成离线安装
- jvm参数设置大全
- kill -9 杀掉oracle所有进程却不能启动和连接数据库的解决办法!
- 使用shell调用python中的函数
- JAVA取数字小数点后N位
- Centos5.2下载安装ZendStudio for Linux
- 李彦宏南大演讲:90后是幸运的一代 机会很多
- CSDN我来了
- 运算^
- SQL语言的分类
- 解决ADB server didn't ACK方法
- notepad++【001】开启代码提示和括号补齐功能