shell腳本中執行python腳本並接收其返回值的例子


1.在shell腳本執行python腳本時,需要通過python腳本的返回值來判斷后面程序要執行的命令

例:有兩個py程序  hello.py

復制代碼代碼如下:

def main():
    print "Hello"

 

if __name__=='__main__':
    main()
world.py

def main():
    print "Hello"

if __name__=='__main__':
    main()


shell 腳本 test.sh

復制代碼代碼如下:

python hello.py
python world.py


執行sh test.sh 打印結果為

復制代碼代碼如下:

  hello
  world


在hello.py中通過返回值  讓shell腳本通過參數來判斷,

 

hello.py這樣寫

 

復制代碼代碼如下:

import sys

 

def main():
    try:
        print "hello"
        sys.exit(0)
    except:
        sys.exit(1)

if __name__=='__main__':
    main()

 

shell 腳本改為

復制代碼代碼如下:

python hello.py
if [ $?==0 ];then
    exit
else
        python world.py        
fi


就可以判斷了

 

sh腳本中執行了python腳本,如mysh.sh文件:

python "mypy.py"

result = $?

 

result就是調用python執行的結果。

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM