Using code snippets

All we mean by the term code snippets is a prepared code that we can read into our current script. This is especially easy with vim being able to read the contents of other text files during editing:

ESC
:r <path-and-filename>

For example, if we need to read the contents of a file called if located in $HOME/snippets, we will use the following key sequences in vim:

ESC
:r $HOME/snippets/if

The contents of this file is read into the current document below the current cursor position. In this way, we can make the code snippets as complex as we need and maintain the correct indentations to aide readability and consistency.

So, we will make it our duty to always create a snippets directory in our home directory:

$ mkdir -m 700 $HOME/snippets

It is not required to share the directory, so it is good practice to set the mode to 700 or private to the user when it is being created.

When creating snippets, it is your choice to use a pseudo-code or real examples. My preference is to use real examples that are edited to reflect the requirements of the recipient script. The contents of a simple if snippet will be:

if [ -z $1 ] ; then
    echo "Usage: $0 <name>"
    exit 2
fi

This gives us the layout to create an if statement with a practical example. In this case, we check to see if $1 is unset and send an error to the user before exiting the script. The key is in keeping the snippet short to limit the changes that need to be made but easily understood and expandable, as required.

Bringing color to the terminal

If we are to display text messages to the users and operators executing the scripts, we can provide colors to help in message interpretation. Using red as a synonym for errors and green indicating success makes it easier to add functionality to our scripts. Not all but certainly a vast majority of Linux terminals support color. The built-in command echo when used with the -e option can display color to users.

To display a text in red we can use the echo command, as follows:

$ echo -e "33[31mError33[0m"

The following screenshot shows both the code and the output:

Bringing color to the terminal

The red text will bring immediate attention to the text and the potential of failure of script execution. The use of color in this way adheres to the basics of principals application design. If you find the code cumbersome, then simply use friendly variables to represent the colors and the reset code.

In the previous code, we used red and the final rest code to set the text back to the shell default. We could easily create variables for these color codes and others:

RED="33[31m"
GREEN="33[32m"
BLUE="33[34m"
RESET="33[0m"

Tip

The 33 value is the ESCAPE character and [31m is the color code for red.

We need to take care while using variables, to ensure that they are properly delimited from the text. Modifying the earlier example, we can see how this is easily achieved:

$ echo -e ${RED}Error$RESET"

Tip

We use the brace brackets to ensure that the RED variable is identified and separated from the Error word.

Saving the variable definitions to the $HOME/snippets/color file will allow them to be used in other scripts. Interestingly, we don't need to edit this script; we can use the command source to read these variables definitions into the script at runtime. Within the recipient script, we need to add the following line:

source $HOME/snippets/color

Using the shell built-in source command will read the color variables into the script executing at runtime. The following screenshot shows a modified version of the hello5.sh script that we now call hello7.sh, which makes use of these colors:

Bringing color to the terminal

We can see the effect this has when we execute the script. In the following screenshot, you will see the execution and output both with and without a supplied parameter:

Bringing color to the terminal

We can easily identify the success and failure of the script via the color coded output; the green Hello fred where we supply the parameter and the red Usage statement where we have not provided the required name.

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

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