In an answer to a previous question:
How can I use 'do I have root access?' as a conditional in bash?
The suggestion to 'try to do something and detect if it fails' instead of 'check permission and then do something'
I have found plenty of rationale for this e.g.:
- Thorough use of 'if' statements or 'try/catch' blocks?
- What is the advantage of using try {} catch {} versus if {} else {}
However, I have found very little clear information about how to implementing try/catch in bash. I would guess that it is too easy, except that what I have found seems rather complicated- using functions or other scripts:
I am relatively new to bash but confused that there is not a simple function similar to the try
function in other languages.
specifically, I would like to do the following:
CMD=`./path/to/script.sh`
if [ <echo $CMD | grep error is true> ]; then
.. do this ..
else
.. do that ..
fi