這篇文章主要介紹“怎么在shell腳本中執(zhí)行python腳本并接收其返回值”,在日常操作中,相信很多人在怎么在shell腳本中執(zhí)行python腳本并接收其返回值問題上存在疑惑,小編查閱了各式資料,整理出簡單好用的操作方法,希望對(duì)大家解答”怎么在shell腳本中執(zhí)行python腳本并接收其返回值”的疑惑有所幫助!接下來,請(qǐng)跟著小編一起來學(xué)習(xí)吧!
1.在shell腳本執(zhí)行python腳本時(shí),需要通過python腳本的返回值來判斷后面程序要執(zhí)行的命令
例:有兩個(gè)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
執(zhí)行sh test.sh 打印結(jié)果為
代碼如下:
hello
world
在hello.py中通過返回值 讓shell腳本通過參數(shù)來判斷,
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
到此,關(guān)于“怎么在shell腳本中執(zhí)行python腳本并接收其返回值”的學(xué)習(xí)就結(jié)束了,希望能夠解決大家的疑惑。理論與實(shí)踐的搭配能更好的幫助大家學(xué)習(xí),快去試試吧!若想繼續(xù)學(xué)習(xí)更多相關(guān)知識(shí),請(qǐng)繼續(xù)關(guān)注創(chuàng)新互聯(lián)網(wǎng)站,小編會(huì)繼續(xù)努力為大家?guī)砀鄬?shí)用的文章!