check.py
from subprocess import Popen, PIPE
import sys
cmd = "az image show --resource-group ****** --name bla"
args = cmd.split()
#sub_proc = subprocess.Popen(args, stdout=subprocess.PIPE, stderr=subprocess.PIPE, shell=False)
p = Popen(args, stdout=PIPE, stderr=PIPE)
(out,err) = p.communicate();
rc = p.wait()
print ("output:\n\n%s\n\n" % out)
print ("errors:\n\n%s\n\n" % err)
print ("returned code = %d" % p.returncode)
sys.exit(rc)
test.sh
#! /bin/bash -xe
safeRun(){
cmd="az image show --resource-group ******* --name blba -o table"
eval $cmd
rc=$?
echo $rc
if [ $rc == 3 ]; then
echo "no such image"
fi
return $rc
}
safeRun
echo 'print the result'
echo $rc