More complexity

A function can have as many commands as needed. In our simple example, we only added a single echo, which we then only called once. While this is nice for abstraction, it does not really warrant creating a function (yet). Let's look at a more complex example that will give you a better idea why abstraction of commands in functions is a good idea:

reader@ubuntu:~/scripts/chapter_13$ vim complex-function.sh 
reader@ubuntu:~/scripts/chapter_13$ cat complex-function.sh
#!/bin/bash

#####################################
# Author: Sebastiaan Tammer
# Version: v1.0.0
# Date: 2018-11-11
# Description: A more complex function that shows why functions exist.
# Usage: ./complex-function.sh
#####################################

# Used to print some current data on the system.
print_system_status() {
date # Print the current datetime.
echo "CPU in use: $(top -bn1 | grep Cpu | awk '{print $2}')"
echo "Memory in use: $(free -h | grep Mem | awk '{print $3}')"
echo "Disk space available on /: $(df -k / | grep / | awk '{print $4}')"
echo # Extra newline for readability.
}

# Print the system status a few times.
for ((i=0; i<5; i++)); do
print_system_status
sleep 5
done

Now we're talking! This function has five commands, three of which include command substitution with chained pipes. Now, our scripts are starting to become complex yet powerful. As you can see, we define the function using the () notation. We then call this function in a C-style for loop, which causes the script to print the system status five times with a five-second pause in between (due to sleep, which we saw earlier in Chapter 11, Conditional Testing and Scripting Loops). When you run this, it should look like this:

reader@ubuntu:~/scripts/chapter_13$ bash complex-function.sh 
Sun Nov 11 13:40:17 UTC 2018
CPU in use: 0.1
Memory in use: 85M
Disk space available on /: 4679156

Sun Nov 11 13:40:22 UTC 2018
CPU in use: 0.2
Memory in use: 84M
Disk space available on /: 4679156

Apart from the date, the chance of the other output changing significantly is slim, unless you have other processes running. However, the purpose of functions should be clear: define and abstract a set of functionalities in a transparent manner.

While not the topic of this chapter, we used a few new commands here. The top and free commands are often used to check how the system is performing, and can be used without any arguments (top opens full screen, which you can exit with Ctrl C). In the Further reading section of this chapter, you can find more on these (and other) performance monitoring tools in Linux. We've also included a primer on awk there.

There are many advantages to using functions; these include but are not limited to the following:

  • Easy to reuse code
  • Allows sharing of code (via libraries, for example)
  • Abstract confusing code to a simple function call

An important thing in functions is naming. A function name should be as concise as possible, but still needs to tell the user what it does. For example, if you call a function something non-descriptive such as function1, how will anyone know what it does? Compare this to a name like we saw in the example: print_system_status. While perhaps not perfect (what is system status?), it at least points us in the right direction (if you agree that CPU, memory, and disk usage are considered part of system status, that is). Perhaps a better name for the function would be print_cpu_mem_disk. It is up to you to decide! Make sure you consider who the target audience is when making this choice; this often has the greatest impact.

While descriptiveness is very important in function naming, so is adhering to a naming convention. We've already presented this same consideration in Chapter 8, Variables and User Input, when we dealt with variable naming. To reiterate: the most important rule is to be consistent. If you want our advice for a function naming convention, stick with the one we laid out for variables: lowercase, separated by underscores. This is what we used in the previous examples, and is what we will continue to show in the rest of the book.

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

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