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
就可以判斷了
新聞熱點
疑難解答