简体   繁体   中英

Exit the shell script from embedded python code

I want to exit the shell script from the embedded python code. Below os.system() is not working and rest of the shell scripting code is still getting executed.

echo "this is shell code"
a=10
python << END
import os
print "this is python code"

if True:
        print "yes"
        print $a
        os.system("exit 1")
END
echo "I am out of python code"

You have to exit the shell explicitly based on the result of the Python script.

echo "this is shell code"
a=10
python "$a" <<END || exit
print "this is python code"
if True:
    print "yes"
    print(sys.argv[1])
    sys.exit(1)
END
echo "I am out of python code"

In this example, where the Python code unconditionally exits with a status of 1, the final echo will not be reached, as the nonzero exit status will cause the shell command exit to run.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM