How to check the exit status of an application after using 'open' in shell

I've looked all over for this and I'm not entirely sure how to do this.

I want to use a bash script to open an application using 'open' and then check the exit status of the application after it has finished.

As far as I am aware, open -W will exit with status 0 so long as the application did open, I'm not interested in this exit, I'm interested in the apps exit.

Currently I have the following script:

trap "echo manual abort; exit 1"  1 2 3 15; 
while open -W /path/to/MyApp.app
    echo "all is well"
done

exit 0 

Which opens the app and then waits, if the exit status is 0 it will re-open the app again after closing. This is intended however, what I want is to be able to stop this script if the app itself didn't exit 0 rather than the command 'open' successfully exiting.

So in short:

Open MyApp.app

if MyApp.app (not open) crashes, stop the script, otherwise, re-open MyApp.app


My answer is: you can't with open even with the -W flag. Rather use a shell script repeat_run which might be built as follows:

cat >repeat_run.sh <<'__eof__'
#!/bin/sh
app_name=$1
bin_name=`defaults read /Applications/${app_name}.app/Contents/Info.plist CFBundleExecutable 2>/dev/null`
if [ "${bin_name}" = "" ] ; then
    echo "${app_name} not found" >&2
    exit 2
fi
bin_path=/Applications/${app_name}.app/Contents/MacOS/${bin_name}
trap "echo manual abort ; exit 1"  1 2 3 15
while : ; do
    ${bin_path}
    rc=$?
    case $rc in
        0)      echo "$1 terminated correctly" ;;
        *)      exit ${rc} ;;
    esac
done
__eof__
make repeat_run
./repeat_run Pages