We've seen that one way to interact with the Linux kernel is through the shell, by invoking commands. A command can fail, as we can imagine, and a way to communicate a failure is to return a non-negative integer value. 0, in most cases, means success. This recipe will show you how to deal with error handling on the shell.
Handling a Linux bash error
How to do it...
This section will show you how to get errors directly from the shell and via a script, which is a fundamental aspect of script development:
- First, run the following command:
root@e9ebbdbe3899:/# cp file file2
cp: cannot stat 'file': No such file or directory
root@e9ebbdbe3899:/# echo $?
1
- Create a new file called first_script.sh and type in the following code:
#!/bin/bash
cat does_not_exists.txt
if [ $? -eq 0 ]
then
echo "All good, does_not_exist.txt exists!"
exit 0
else
echo "does_not_exist.txt really DOES NOT exists!!" >&2
exit 11
fi
- Save the file, and exit (:wq or :x).
- Give execution permission (the x flag) to the current user for the first_script.sh file:
root@e9ebbdbe3899:~# chmod u+x first_script.sh
These steps are detailed in the next section.
How it works...
In step 1, the cp command failed, as file and file2 don't exist. By querying echo $?, we get the error code; in this case, it is 1. This is particularly useful when writing bash scripts where we might need to check for a particular condition.
In step 2, the script just lists the does_not_exist.txt file and reads the error code returned. If all goes fine, it prints an acknowledgment message and returns 0. Otherwise, it returns the error code 11.
By running the script, we get the output as follows:
Here, we notice a couple of things:
- We logged our error string.
- The error code is the one we had set in the script.
Under the hood, every time a command is invoked, it enters into kernel space. The command is executed, and a return status is sent back to the user in the form of an integer. It's really important to consider this return status, as we might have a command that apparently succeeded (no output) but eventually failed (returns code different from 0).
There's more...
One important aspect of the return status of the commands is that it can be used to (conditionally) run the next command. Two important operators are used for this purpose: && (AND) and || (OR).
In the two commands here, the second is run if—and only if—the first succeeds (the && operator). file.txt is removed if it is copied to the project folder:
cp file.txt ~/projects && rm -f file.txt
Let's have a look at a second example:
cp file.txt ~/projects || echo 'copy failed!'
In the preceding example, the second command is run only if the first fails (the || operator). copy failed! is printed if the copy fails.
In this recipe, we just showed that commands can be combined on a shell script to create a more complex command, and by controlling the error code, we can control the flow of execution. Man pages are a great resource as they contain all the commands and error codes (for example, man cp and man cat).