zoukankan      html  css  js  c++  java
  • shell调用python脚本,并且向python脚本传递参数

    1.shell调用python脚本,并且向python脚本传递参数:

    shell中: python test.py $para1 $para2 python中:
    import sys def main($canshu1, $canshu2) ..... main(sys.argv[1], sys.argv[2])

    2.使用shell调用python中的函数:

    python脚本如下:

    test.py:

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

    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)  
    本文转载自:https://blog.csdn.net/tanlon_0308/article/details/40423751
  • 相关阅读:
    批量插入测试脚本
    Show Profile分析sql语句的资源消耗
    慢查询日志
    ORDER BY优化
    Join查询
    Explain(执行计划)分析
    索引
    MySQL中的DML(数据操作语言)和DQL(数据查询语言)
    MySQL中的DDL(数据定义语言)和DCL(数据控制语言)
    MySQL架构体系介绍
  • 原文地址:https://www.cnblogs.com/momoyan/p/9145871.html
Copyright © 2011-2022 走看看