shell腳本執行python腳本時,python如何將返回值傳給shell腳本


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

就可以判斷了


免責聲明!

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



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