Nesting

Another concept that is very interesting is nesting. In essence, nesting is really simple: it is placing another if-then-else statement within either the then or else of the outer if-then-else. This allows us to, for example, first determine if a file is readable, before determining what type of file it is. By using nested if-then-else statements, we can rewrite the previous code in such a way that we no longer need the || construct:

reader@ubuntu:~/scripts/chapter_11$ vim nested-print-or-list.sh 
reader@ubuntu:~/scripts/chapter_11$ cat nested-print-or-list.sh
#!/bin/bash

#####################################
# Author: Sebastiaan Tammer
# Version: v1.0.0
# Date: 2018-10-26
# Description: Prints or lists the given path, depending on type.
# Usage: ./nested-print-or-list.sh <file or directory path>
#####################################

# Since we're dealing with paths, set current working directory.
cd $(dirname $0)

# Input validation.
if [[ $# -ne 1 ]]; then
echo "Incorrect usage!"
echo "Usage: $0 <file or directory path>"
exit 1
fi

input_path=$1

# First, check if we can read the file.
if [[ -r ${input_path} ]]; then
# We can read the file, now we determine what type it is.
if [[ -f ${input_path} ]]; then
echo "File found, showing content:"
cat ${input_path}
elif [[ -d ${input_path} ]]; then
echo "Directory found, listing:"
ls -l ${input_path}
else
echo "Path is neither a file nor a directory, exiting script."
exit 1
fi
else
# We cannot read the file, print an error.
echo "Cannot read the file/directory, exiting script."
exit 1
fi

Try the preceding script with the same input as the previous example. In this case, you'll see much nicer output in the error scenarios, since we now control those (instead of the default output of cat: /etc/shadow: Permission denied from cat, for example). Functionally, however, nothing has changed! We think that this script, which uses nesting, is more readable than the previous example, because we handle the error scenarios ourselves now instead of relying on the system commands to do it for us.

We've discussed indentation before, but in our opinion, scripts like this one are where it truly shines. By indenting the inner if-then-else statement, it is much more clear that the second else belongs to the outer if-then-else statement. If you're using multiple levels of indentation (because, in theory, you can nest as often as you'd like), it really helps everyone working on the script to follow this logic.

Nesting is not just reserved for if-then-else. The two loops that we will introduce later in this chapter, for and while, can also be nested. And, what's even more practical, you can nest all of them within all of the others (from a technical perspective; it should make sense from a logical perspective as well, of course!). You will see examples of this when we explain while and for later.

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

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