Shorthand syntax

By now, we've seen a few uses of an if block to see if our previous commands ran successfully. While the functionality is great, using 5-7 lines after each command where you suspect errors could occur really adds to the total script length! Even more of an issue will be readability: if half the script is error checking, it might be very hard to get to the bottom of the code. Fortunately, there is a way in which we can check for errors directly after a command. We can accomplish this with the || command, which is the Bash version of a logical OR. Its counterpart, &&, is the implementation of a logical AND. To illustrate this, we'll introduce two new commands: true and false. If you take a look at the respective man pages, you'll find the clearest answer you can possibly get:

  • true: Do nothing, successfully
  • false: Do nothing, unsuccessfully

The following script illustrates how we use || and && to create a logical application flow. If logical operators are unfamiliar terrain, check out the link in the Further reading section under Logical operators first:

reader@ubuntu:~/scripts/chapter_09$ vim true-false.sh 
reader@ubuntu:~/scripts/chapter_09$ cat true-false.sh
#!/bin/bash

#####################################
# Author: Sebastiaan Tammer
# Version: v1.0.0
# Date: 2018-10-02
# Description: Shows the logical AND and OR (&& and ||).
# Usage: ./true-false.sh
#####################################

# Check out how an exit status of 0 affects the logical operators:
true && echo "We get here because the first part is true!"
true || echo "We never see this because the first part is true :("

# Check out how an exit status of 1 affects the logical operators:
false && echo "Since we only continue after && with an exit status of 0, this is never printed."
false || echo "Because we only continue after || with a return code that is not 0, we see this!"

reader@ubuntu:~/scripts/chapter_09$ bash -x true-false.sh
+ true
+ echo 'We get here because the first part is true!'
We get here because the first part is true!
+ true
+ false
+ false
+ echo 'Because we only continue after || with a return code that is not 0, we see this!'
Because we only continue after || with a return code that is not 0, we see this!

As we expect, the code after && is only executed if the command before returns an exit code of 0, while the code after || is only executed if the exit code is not 0 (so, most often, 1). If you look closely, you can actually see this happening in the debug of the script. You can see true being executed twice, as well as false. However, the first echo we actually end up seeing is after the first true, whereas the second echo we see is after the second false! We've highlighted this in the preceding code for your convenience.

Now, how can we use this to handle errors? An error will give an exit status that is anything other than 0, so this is comparable to the false command. In our example, the code after the logical operator || was printed after the false. This makes sense, because either false OR echo should succeed. In this case, since false (by default) fails, echo is executed. In the following simple example, we'll show you how we would use the || operator in a script:

reader@ubuntu:~/scripts/chapter_09$ vim logical-or.sh
reader@ubuntu:~/scripts/chapter_09$ cat logical-or.sh
#!/bin/bash

#####################################
# Author: Sebastiaan Tammer
# Version: v1.0.0
# Date: 2018-10-02
# Description: Use the logical OR for error handling.
# Usage: ./logical-or.sh
#####################################

# This command will surely fail because we don't have the permissions needed:
cat /etc/shadow || exit 123

reader@ubuntu:~/scripts/chapter_09$ cat /etc/shadow
cat: /etc/shadow: Permission denied
reader@ubuntu:~/scripts/chapter_09$ echo $?
1
reader@ubuntu:~/scripts/chapter_09$ bash logical-or.sh
cat: /etc/shadow: Permission denied
reader@ubuntu:~/scripts/chapter_09$ echo $?
123

We try to cat a file that we do not have permissions to (which is a good thing, since /etc/shadow contains the hash passwords for all users on the system). When we do this normally, we receive the exit status of 1, as you can see from our manual cat. However, in our script, we use exit 123. If our logical operator does its job, we will not exit with the default 1, but instead with exit status 123. When we call the script, we get the same Permission denied error, but this time when we print the return code, we see the expected 123.

If you really want to confirm that the code after || is only executed if the first part fails, run the script with sudo. In this case, you will see the contents of /etc/shadow, since root has those permissions and the exit code will be 0 instead of the earlier 1 and 123.

Similarly, you can also use && if you only want to execute code when you're entirely sure the first command has finished successfully. To handle potential errors in a really graceful manner, it would be best to combine echo and exit after the ||. In the next example, on one of the next few pages, you will see how this is achieved! We will use that way of handling errors in the rest of this book, so don't worry about the syntax just yet – you will encounter it many more times before this book is over.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.118.93.64