Example of executing a Python script in a shell script and receiving its return value _linux shell

Source: Internet
Author: User
Tags python script

1. When the shell script executes the Python script, you need to use the return value of the Python script to determine the command to be executed by the following program

Example: There are two py programs hello.py

Copy Code code as follows:

def main ():
Print "Hello"

If __name__== ' __main__ ':
Main ()
world.py

def main ():
Print "Hello"

If __name__== ' __main__ ':
Main ()


Shell Script test.sh
Copy Code code as follows:

Python hello.py
Python world.py

Perform SH test.sh print results to
Copy Code code as follows:

Hello
World

In hello.py, the shell script is judged by its parameters by the return value,

Hello.py wrote this.

Copy Code code as follows:

Import Sys

def main ():
Try
print "Hello"
Sys.exit (0)
Except
Sys.exit (1)

If __name__== ' __main__ ':
Main ()

The shell script is changed to

Copy Code code as follows:

Python hello.py
if [$?==0];then
Exit
Else
Python world.py
Fi

We can tell.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.