Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to execute a python script in a shell script and receive its return value

2025-04-07 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/03 Report--

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!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 287

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report