十年網(wǎng)站開發(fā)經(jīng)驗 + 多家企業(yè)客戶 + 靠譜的建站團隊
量身定制 + 運營維護+專業(yè)推廣+無憂售后,網(wǎng)站問題一站解決
這篇文章主要介紹“怎么在shell腳本中執(zhí)行python腳本并接收其返回值”,在日常操作中,相信很多人在怎么在shell腳本中執(zhí)行python腳本并接收其返回值問題上存在疑惑,小編查閱了各式資料,整理出簡單好用的操作方法,希望對大家解答”怎么在shell腳本中執(zhí)行python腳本并接收其返回值”的疑惑有所幫助!接下來,請跟著小編一起來學習吧!
1.在shell腳本執(zhí)行python腳本時,需要通過python腳本的返回值來判斷后面程序要執(zhí)行的命令
例:有兩個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 打印結果為
代碼如下:
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
到此,關于“怎么在shell腳本中執(zhí)行python腳本并接收其返回值”的學習就結束了,希望能夠解決大家的疑惑。理論與實踐的搭配能更好的幫助大家學習,快去試試吧!若想繼續(xù)學習更多相關知識,請繼續(xù)關注創(chuàng)新互聯(lián)網(wǎng)站,小編會繼續(xù)努力為大家?guī)砀鄬嵱玫奈恼拢?/p>
網(wǎng)站欄目:怎么在shell腳本中執(zhí)行python腳本并接收其返回值-創(chuàng)新互聯(lián)
網(wǎng)址分享:http://m.jiaotiyi.com/article/ddgcdi.html