Chapter 5. Alternative Syntax

So far in the scripting journey, we have seen that we can use the test command to determine a conditional status. We have taken this a little further and discovered that we can also make use of the single square bracket. Here, we will recap the test command and look at the single square bracket in more detail. After having learned more about the square bracket, we will move onto more advanced variable or parameter management; thus, providing defaults and understating quoting issues.

Finally, we are going to see that within advanced shells like bash, korn, and zsh we can go with double brackets! Making use of the double round parenthesis and double square bracket can simplify the overall syntax and allow the standardization of the use of mathematical symbols.

In this chapter, we will cover the following topics:

  • Test conditions
  • Providing parameter defaults
  • When in doubt – quote!
  • Advanced tests using [[
  • Advanced tests using ((

Recapping test

So far we have used the built-in test command to drive our conditional statements. Using other options with test, we can look at the returned value to determine the status of files in the file system. Running the test without any option will return a false output:

$ test

Testing files

Commonly, we can use test to check the conditions based around files. For example, to test that a file is present, or not, we can use the-e option. The following command will test the existence of the /etc/hosts file:

test -e /etc/hosts

We can run this test again, but this time check that the file not only exists but is a regular file as opposed to having some special purpose. Specific file types can be directories, pipes, links, and so on. The option for a regular file is -f.

$ test -f /etc/hosts

Adding logic

If we need to open a file from within our script, we will test that the file is both a regular file and has the read permission set. To achieve this with test, we can also include the -a option to AND multiple conditions together. In the following example code, we will use the -r condition to check that the file is readable:

$ test -f /etc/hosts -a -r /etc/hosts

Similarly, the use of -o is supported to OR two conditions within an expression.

Square brackets as not seen before

As an alternative to the test command, we can implement the same conditional tests using the single square bracket. Repeating the previous conditional test and omitting the command itself. We will rewrite this, as shown in the following code:

 $ [ -f /etc/hosts -a -r /etc/hosts ]

Many times, even as experienced administrators, we are used to language elements and we accept them as they are. I feel many Linux administrators will be surprised to learn that [ is a command for both a shell built-in and a standalone file. Using the type command we can verify this:

$ type -a [

We can see the output of this command in the following screenshot confirming its existence:

Square brackets as not seen before

We can see that on the Raspbian distribution that I am using, there is the built-in [ command and the /usr/bin/[ command. As we have seen, both these commands imitate the test command but it requires a closing bracket.

Now we know a little more about the [ command, which is found in bash and the earlier Bourne shell; we can now continue to add a little command-line list syntax. In addition to the command-line list, we can see the desired functionality working in the following code sample:

$ FILE=/etc/hosts
$ [ -f $FILE -a -r $FILE ] && cat $FILE

Having set the parameter FILE variable, we can test that it is both a regular file and is readable by the user before attempting to list the file contents. In this way, the script becomes more robust without the need for a complex script logic. We can see the code in use in the following screenshot:

Square brackets as not seen before

This type of abbreviation is quite common and is easily recognizable. We should always be cautious of using abbreviations if they do not add readability. Our aim in scripting should be to write a clear and understandable code and avoid shortcuts if they do not add to this goal.

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

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