Ignoring specific errors in a shell script
Solution 1
In order to cause bash to ignore errors for specific commands you can say:
some-arbitrary-command || true
This would make the script continue. For example, if you have the following script:
$ cat foo
set -e
echo 1
some-arbitrary-command || true
echo 2
Executing it would return:
$ bash foo
1
z: line 3: some-arbitrary-command: command not found
2
In the absence of || true
in the command line, it'd have produced:
$ bash foo
1
z: line 3: some-arbitrary-command: command not found
Quote from the manual:
The shell does not exit if the command that fails is part of the command list immediately following a
while
oruntil
keyword, part of the test in anif
statement, part of any command executed in a&&
or||
list except the command following the final&&
or||
, any command in a pipeline but the last, or if the command’s return status is being inverted with!
. A trap onERR
, if set, is executed before the shell exits.
EDIT: In order to change the behaviour such that in the execution should continue only if executing some-arbitrary-command
returned file not found
as part of the error, you can say:
[[ $(some-arbitrary-command 2>&1) =~ "file not found" ]]
As an example, execute the following (no file named MissingFile.txt
exists):
$ cat foo
#!/bin/bash
set -u
set -e
foo() {
rm MissingFile.txt
}
echo 1
[[ $(foo 2>&1) =~ "No such file" ]]
echo 2
$(foo)
echo 3
This produces the following output:
$ bash foo
1
2
rm: cannot remove `MissingFile.txt': No such file or directory
Note that echo 2
was executed but echo 3
wasn't.
Solution 2
Use:
command || :
: 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.
Related videos on Youtube
Stunt
Updated on July 22, 2022Comments
-
Stunt almost 2 years
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
-
S.R.I almost 11 yearsDoes this help? And, Welcome to SO! :)
-
Stunt almost 11 yearsThat does help to improve my error reporting however it does not include anything for handling different errors in a different manor. I'm essentially trying to mimic the try{}catch{} from c#
-
-
Stunt almost 11 yearsIs this more of a replacement for my "set +e and set -e" usage? I'm not sure i understand how this handles varying errors.
-
devnull almost 11 years@Stunt This isn't a replacement. It just implies that if you had
set -e
at the beginning of the script, you don't need to sayset +e
to ignore errors. Sayingcommand || true
would suffice. -
Stunt almost 11 yearsThat's helpful for cleaning up my code (The set +e and set -e can get quite messy!) However it's still handling errors on a more general basis. For this question i'd like to inspect the error and then continue or exit based on which error is thrown. (Can't see how to vote up your comment as helpful!)
-
Stunt almost 11 yearsThat looks like it could be exactly what i need! However, on testing it's syntax errors. Here's my test: #!/bin/bash set -u set -e foo() { rm MissingFile.txt } echo 1 [[ $(foo 2>&1) =~ "No such file" ]] echo 2 This produces a '(' expected error. (I've used a function here as i suspect this will be required for my example).
-
Stunt almost 11 yearsSorry my error i reported should say '(' UNexpected. It seems to be complaining about the ( before "foo"
-
devnull almost 11 yearsNot sure what's causing the error for you. See the edit above for your test.
-
Stunt almost 11 yearsThe edit looks great, however if you change the text from "File not found" to "bbb" (Which should never return"). You will receive "2" on the output. This suggests to me that the error checking may not be working correctly
-
devnull almost 11 yearsDidn't you say that you wanted to continue only if the error matched a pattern? The script stops because your error doesn't match the pattern
bbb
and returns with a non-zero exit code. -
Stunt almost 11 yearsSorry, i should have been clearer. My test showed that the test did not produce a non-zero exit code as it successfully outputted the next line "2". I would be looking for only "1" to be outputted in this scenario.
-
devnull almost 11 yearsLooks like you're using an older version of
bash
. This should work well with bash 4+. -
Stunt almost 11 yearsVersion 3.00.16 installed at the moment
-
Stunt almost 11 yearsApologies it took so long, i've only just managed to test this on 4.2. This solution has worked. Thanks! :)
-
Mecki over 7 yearsTrue is only an external program if you run
/bin/true
, astrue
on its own is a shell built-in of bash, just like echo and test, which also exists as external programs but are also built-ins in bash and if you don't give a full path, the built-ins will be used. -
Sandra almost 2 yearsif you get confused easily like me, LHS = left hand side, RHS = right hand side (nothing to do with flowers or schools in this case) (: