Using the test shell builtin

It is probably time for us to pull over to the side of the scripting highway and look a little more at this command test. This is both a shell builtin and a file executable in its own right. Of course, we will have to hit the built-in command first, unless we specify the full path to the file.

When the test command is run without any expressions to evaluate, then the test will return false. So, if we run the test as shown in the following command:

$ test

The exit status will be 1, even though no error output is shown. The test command will always return either True or False or 0 or 1, respectively. The basic syntax of test is:

test EXPRESSION

Or, we can inverse the test command with:

test ! EXPRESSION

If we need to include multiple expressions, these can be AND or OR together using the -a and -o options, respectively:

test EXPRESSION -a EXPRESSION
test EXPRESSION -o EXPRESSION

We can also write in a shorthand version replacing the test with square brackets to surround the expression as shown in the following example:

[ EXPRESION ]

Testing strings

We can test for the equality or inequality of two strings. For example, one of the ways to test the root user is using the following command:

test $USER = root

We could also write this using the square bracket notation:

[ $USER = root ]

Equally, we could test for a non-root account with the following two methods:

test ! $USER = root
[ ! $USER = root ]

We can also test for zero values and non-zero values of strings. We saw this in an earlier example in this chapter.

To test if a string has a value, we could use the -n option. We can check to see if the current connection is made via SSH by checking for the existence of a variable in the user's environment. We show this using test and square brackets in the following two examples:

test -n $SSH_TTY
[ -n $SSH_TTY ]

If this is true, then the connection is made with SSH; if it is false, then the connection is not via SSH.

As we saw earlier, testing for a zero string value is useful when deciding if a variable is set:

test -z $1

Or, more simply, we could use:

[ -z $1 ]

A true result for this query means that no input parameters have been supplied to the script.

Testing integers

As well as, testing string values of bash scripts can test for integer values and whole numbers. Another way of testing input of a script is to count the numbers of positional parameters and also test that the number is above 0:

test $# -gt 0

Or using the brackets, as shown:

[ $# -gt 0 ]

When in relationship, top positional parameters the variable $# represents the number of parameters passed to the script. To test equality of integer values, the -eq option is used and not the = symbol.

Testing file types

While testing for values we can test for the existence of a file or file type. For example, we may only want to delete a file if it is a symbolic link. I use this while compiling a kernel. The /usr/src/linux directory should be a symbolic link to the latest kernel source code. If I download a newer version before compiling the new kernel, I need to delete the existing link and create a new link. Just in case someone has created the /usr/src/linux directory, we can test it as a link before removing it:

# [ -h /usr/src/linux ] &&rm /usr/src/linux

The -h option tests that the file has a link. Other options include:

  • -d: This shows that it's a directory
  • -e: This shows that the file exists in any form
  • -x: This shows that the file is executable
  • -f: This shows that the file is a regular file
  • -r: This shows that the file is readable
  • -p: This shows that the file is a named pipe
  • -b: This shows that the file is a block device
  • -c: This shows that the file is a character device

More options do exist, so delve into the main pages as you need. We will use different options throughout the book; thus, giving you practical and useful examples.

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

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