Use bash internal variable $? to get exit status of commands
"Within bash, the special parameter $? is expanded to the exit status of the last executed command."
Keep in mind, if you pipeline several commands, the exit status of the last command to execute will be the return status of the entire pipeline. Also keep in mind that $? will be overwritten almost immediately if you don't save it off somewhere.
So, you can either save $? like this:
ldapsearch blah blah blah
rc=$?
or use a normal bash if/else structure like this:
if ldapsearch blah blah blah
then
do stuff if ldapsearch succeeds
else
do stuff if ldapsearch fails
fi
Remember, bash's "if" does not test for expression validity, like most languages - it tests for process success or failure. The use of the "test" command or brackets lets you provide success or failure based on expression evaluation, though.
Hope that helps!
|