shell可以执行python吗


本文摘自php中文网,作者anonymity,侵删。

shell可以执行python吗?

实际案例:shell调用python脚本,并且向python脚本传递参数

shell中:

1

python test.py $para1 $para2

python中:

1

2

3

4

import sys

def main($canshu1, $canshu2)

  .....

main(sys.argv[1], sys.argv[2])

使用shell调用python中的函数:

python脚本如下,test.py:

1

2

3

4

5

6

7

8

9

10

import ConfigParser 

   

config = 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中执行一个调用的命令行即可:

1

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

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

为了将结果存储在变量中,你可以因此这样做:

1

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

或者,等效于:

1

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

我们也可以一次调用所有方法,放入一个集合中,再调用切割方法获取相应的值:

1

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

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

1

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

以上就是shell可以执行python吗的详细内容,更多文章请关注木庄网络博客!!

相关阅读 >>

hash()是Python内置的吗

Python数据结构:一个被低估的namedtuple(一)

Python如何对字符串大小写进行转换

Python中flask_migrate,flask_script的使用介绍(附代码)

Python防止sql注入方法介绍

Python的import怎么用

Python中赋值与c语言区别

Python中关于数字的详解

Python怎么让输出在一行

Python实现破解猜数游戏实例详解

更多相关阅读请进入《Python》频道 >>




打赏

取消

感谢您的支持,我会继续努力的!

扫码支持
扫码打赏,您说多少就多少

打开支付宝扫一扫,即可进行扫码打赏哦

分享从这里开始,精彩与您同在

评论

管理员已关闭评论功能...