116

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

2
  • 2
    Does this help? And, Welcome to SO! :)
    – S.R.I
    Jul 24, 2013 at 9:42
  • That 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
    Jul 24, 2013 at 10:00

2 Answers 2

220

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 or until keyword, part of the test in an if 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 on ERR, 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.

12
  • Is this more of a replacement for my "set +e and set -e" usage? I'm not sure i understand how this handles varying errors.
    – Stunt
    Jul 24, 2013 at 10:04
  • 1
    @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 say set +e to ignore errors. Saying command || true would suffice.
    – devnull
    Jul 24, 2013 at 10:07
  • That'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
    Jul 24, 2013 at 10:15
  • That 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
    Jul 24, 2013 at 12:42
  • 1
    Apologies it took so long, i've only just managed to test this on 4.2. This solution has worked. Thanks! :)
    – Stunt
    Jul 25, 2013 at 10:35
49

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.

3
  • 10
    True is only an external program if you run /bin/true, as true 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.
    – Mecki
    Dec 21, 2016 at 13:57
  • 1
    if you get confused easily like me, LHS = left hand side, RHS = right hand side (nothing to do with flowers or schools in this case) (:
    – Sandra
    May 30, 2022 at 13:13
  • What if I need to ignore a piped command return code? E.g. mysqldump | gzip Jun 10, 2023 at 11:53

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.