I’m looking for exception handling mechanism in shell script. Is there any try,catch equivalent mechanism in shell script ?
I’m looking for exception handling mechanism in shell script. Is there any try,catch equivalent
Share
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
There is not really a
try/catchin bash (i assume you’re using bash), but you can achieve a quite similar behaviour using&&or||.In this example, you want to run
fallback_commandifa_commandfails (returns a non-zero value):And in this example, you want to execute
second_commandifa_commandis successful (returns 0):They can easily be mixed together by using a subshell, for example, the following command will execute
a_command, if it succeeds it will then runother_command, but ifa_commandorother_commandfails,fallback_commandwill be executed: