I have a small snippet of a shell script which has the potential to throw many errors. I have the script currently set to globally stop on all errors. However i would like for this small sub-section is slightly different.
Here is the snippet:
recover database using backup controlfile until cancel || true;
auto
I'm expecting this to eventually throw a "file not found" error. However i would like to continue executing on this error. For any other error i would like the script to stop.
What would be the best method of achieving this?
Bash Version 3.00.16
Use:
: is a bash built-in that always returns success. And, as discussed above, || short-circuits so the RHS is only executed if the LHS fails (returns non-zero).
The above suggestions to use 'true' will also work, but are inefficient as 'true' is an external program.
In order to prevent bash to ignore error for specific commands you can say:
This would make the script continue. For example, if you have the following script:
Executing it would return:
In the absence of
|| true
in the command line, it'd have produced:Quote from the manual:
EDIT: In order to change the behaviour such that in the execution should continue only if executing
some-arbitrary-command
returnedfile not found
as part of the error, you can say:As an example, execute the following (no file named
MissingFile.txt
exists):This produces the following output:
Note that
echo 2
was executed butecho 3
wasn't.