How to execute a python script in a shell script and receive its return value
This article mainly introduces "how to execute python script in shell script and receive its return value". In daily operation, I believe many people have doubts about how to execute python script in shell script and receive its return value. Xiaobian consulted all kinds of materials and sorted out simple and easy-to-use operation methods. I hope it will be helpful to answer "how to execute python script in shell script and receive its return value". Next, please follow the editor to study!
1. When the shell script executes the python script, you need to judge the commands to be executed by the later program by the return value of the python script.
Example: there are two py programs hello.py
The code is as follows:
Def main ():
Print "Hello"
If _ _ name__=='__main__':
Main ()
World.py
Def main ():
Print "Hello"
If _ _ name__=='__main__':
Main ()
Shell script test.sh
The code is as follows:
Python hello.py
Python world.py
Perform sh test.sh print result is
The code is as follows:
Hello
World
Let the shell script judge by parameters by returning a value in hello.py
Hello.py wrote like this.
The code is as follows:
Import sys
Def main ():
Try:
Print "hello"
Sys.exit (0)
Except:
Sys.exit (1)
If _ _ name__=='__main__':
Main ()
Change the shell script to
The code is as follows:
Python hello.py
If [$? = = 0]; then
Exit
Else
Python world.py
Fi
At this point, the study on "how to execute a python script in a shell script and receive its return value" is over. I hope to be able to solve your doubts. The collocation of theory and practice can better help you learn, go and try it! If you want to continue to learn more related knowledge, please continue to follow the website, the editor will continue to work hard to bring you more practical articles!