P
P
polyakovyevgeniy2016-08-28 15:02:41
linux
polyakovyevgeniy, 2016-08-28 15:02:41

How to properly handle errors in bash scripts?

How to properly handle errors in autorun scripts? I have such a problem: There is a script for example mounting a hard disk at system startup. But sometimes there is no hard drive, and when the OS is loaded, the script generates an error, and the operating system does not load any further. And you can boot only if you insert this disk. How to write a script so that if, for example, something goes wrong, the system boots further, and does not get stuck and wait for this disk to appear?
Well, I'm interested in a couple of lines of code that can be added to the script, and write the code that I need inside, and if something goes wrong, it will just load further, possibly with an error message displayed in the log. Like in C# try/catch block for example

Answer the question

In order to leave comments, you need to log in

2 answer(s)
D
Dmitry, 2016-08-28
@plin2s

tldp.org/LDP/Bash-Beginners-Guide/html/sect_07_01.html
tldp.org/LDP/Bash-Beginners-Guide/html/sect_07_02.html

J
jcmvbkbc, 2016-08-28
@jcmvbkbc

And there is no universal check for any error? Like this?

the script only exits on an error in the executed command if bash was started with the -e option.
You can undo the effect of this switch by issuing set -e
Or append to a command that may fail with || : like this:
Or wrap the call of such a command in a condition, like this:
if ! do-something ; then
  echo 'do-something failed'
fi

For more information, see man 1 bash , in the section on the set builtin command.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question