$? simply returns the exit code of the previously-executed program. This has two caveats:
1) You MUST call $? immediately after the program you want to test. Now this doesn't mean you have to throw it into your "if" statement immediately, you can save it in a variable instead (eg: status=$?) and then run your test on the variable later on, but you must save the output of $? the very next line after the routine you want to test. If you run anything else (even an 'ls'), $? will contain the exit code of that program instead of the one you're interested in.
2) Since $? is just returning the exit code of the last-executed program, this will only actually do anything if the program you're testing uses exit codes. If the program doesn't use exit codes to identify a successful completion vs error, the calling routing won't be able to use the exit code to do anything.
Last edited by suicidaleggroll; 11-10-2011 at 01:26 AM.