Testing expressions with a test

The shell builtin command test can be used to check file types and compare expressions value. The syntax is test EXPRESSION or the test command is also equivalent to [ EXPRESSION ].

It returns the exit code 1 (false) if the EXPRESSION result is 0, and 0 (true) for a non-zero EXPRESSION result.

If no EXPRESSION is provided, the exit status is set to 1 (false).

File checks

Different kinds of checks can be done on the file using the test command; for example, file existence test, directory test, regular file check, symbolic link check, and so on.

The options available to do various checks on a file are explained in the following table:

Option

Description

-e

fileChecks whether the file exists

-f file

The file is a regular fil

-d file

The file exists and is a directory

-h, -L file

The file is a symbolic link

-b file

The file is block special

-c file

The file is character special

-S file

The file is a socket

-p file

The file is a named pipe

-k file

Sticky bit of the file is set

-g file

set-group-ID (sgid) bit of the file is set

-u file

set-user-id (suid) bit of the file is set

-r file

Read permission on the file exists

-w file

Write permission on the file exists

-x file

Execute permission on the file exists

-t fd

File descriptor fd is open on terminal

file1 -ef file2

file1 is hard link to file2

file1 -nt file2

file1 is more recent compared to file2

file1 -ot file2

The modification time of file1 is older than file2

Shell script performs different checks on the files as follows:

#!/bin/bash
# Filename: file_checks.sh
# Description: Performing different check on and between files

# Checking existence of /tmp/file1
echo -n "Does File /tmp/file1 exist? "
test -e /tmp/file1
echo $?

# Create /tmp/file1
touch /tmp/file1 /tmp/file2
echo -n "Does file /tmp/file1 exist now? "
test -e /tmp/file1
echo $?

# Check whether /tmp is a directory or not
echo -n "Is /tmp a directory? "
test -d /tmp
echo $?

# Checking if sticky bit set on /tmp"
echo -n "Is sticky bit set on /tmp ? "
test -k /tmp
echo $?

# Checking if /tmp has execute permission
echo -n "Does /tmp/ has execute permission ? "
test -x /tmp
echo $?

# Creating another file /tmp/file2
touch /tmp/file2

# Check modification time of /tmp/file1 and /tmp/file2
echo -n "Does /tmp/file1 modified more recently than /tmp/file2 ? "
test /tmp/file1 -nt /tmp/file2
echo $?

The output of running this script is as follows:

Does File /tmp/file1 exist? 1
Does file /tmp/file1 exist now? 0
Is /tmp a directory? 0
Is sticky bit set on /tmp ? 0
Does /tmp/ has execute permission? 0
Does /tmp/file1 modified more recently than /tmp/file2 ? 1

In our output, 0 and 1 are the exist status after running a test command on files. The output 1 means the test failed and 0 means the test was successfully passed.

Arithmetic checks

We can also perform arithmetic checks between integer numbers. Comparison possible on integers is explained to following table:

Comparison

Description

INTEGER1 -eq INTEGER2

INTEGER1 is equal to INTEGER2

INTEGER1 -ne INTEGER2

INTEGER1 is not equal to INTEGER2

INTEGER1 -gt INTEGER2

INTEGER1 is greater than INTEGER2

INTEGER1 -ge INTEGER2

INTEGER1 is greater than or equal to INTEGER2

INTEGER1 -lt INTEGER2

INTEGER1 is lesser than INTEGER2

INTEGER1 -le INTEGER2

INTEGER1 is lesser than or equal to INTEGER2

Shell script shows various arithmetic checks between two integers as follows:

#!/bin/bash
# Filename: integer_checks.sh
# Description: Performing different arithmetic checks between integers

a=12 b=24 c=78 d=24
echo "a = $a , b = $b , c = $c , d = $d"

echo -n "Is a greater than b ? "
test $a -gt $b
echo $?

echo -n "Is b equal to d ? "
test $b -eq $d
echo $?

echo -n "Is c not equal to d ? "
test $c -ne $d
echo $?

The output of running this script is as follows:

a = 12 , b = 24 , c = 78 , d = 24
Is a greater than b ? 1
Is b equal to d ? 0
Is c not equal to d ? 0

Also, here the test returns the exit status after running a comparison test between integers, and returns 0 (true) on success and 1 (false) if the test fails.

String checks

A command test also allows you to perform checks on and between strings. The possible checks are described in the following table:

Comparison

Description

-z STRING

The length of the string is zero

-n STRING

The length of the string is non-zero

STRING1 = STRING2

STRING1 and STRING2 are equal

SRING1 != STRING2

STRING1 and STRING2 are not equal

Shell script shows various string checks on and between strings as follows:

#!/bin/bash
# Filename: string_checks.sh
# Description: Performing checks on and between strings

str1="Hello" str2="Hell" str3="" str4="Hello"
echo "str1 = $str1 , str2 = $str2 , str3 = $str3 , str4 = $str4"

echo -n "Is str3 empty ? "
test -z $str3
echo $?

echo -n "Is str2 not empty? "
test -n $str2
echo $?

echo -n "Are str1 and str4 equal? "
test $str1 = $str4
echo $?

echo -n "Are str1 and str2 different? "
test $str1 != $str2
echo $?

The output of running this script is as follows:

str1 = Hello , str2 = Hell , str3 =  , str4 = Hello
Is str3 empty ? 0
Is str2 not empty? 0
Are str1 and str4 equal? 0
Are str1 and str2 different? 0

Here, the test returns 0 exit status if the string checks are true, else returns 1.

Expression checks

The test command also allows you to perform checks on and between expressions. An expression itself can contain multiple expressions to evaluate as well. The possible checks done are explained in the following table:

Comparison

Description

( EXPRESSION )

This EXPRESSION is true

! EXPRESSION

This EXPRESSION is false

EXPRESSION1 -a EXPRESSION2

Both the expressions are true (the AND operation)

EXPRESSION1 -o EXPRESSION2

Either one of the expressions is true (the OR operation)

Shell script shows various string checks on and between strings as follows:

#!/bin/bash
# Filename: expression_checks.sh
# Description: Performing checks on and between expressions

a=5 b=56
str1="Hello" str2="Hello"

echo "a = $a , b = $b , str1 = $str1 , str2 = $str2"
echo -n "Is a and b are not equal, and str1 and str2 are equal? "
test ! $a -eq $b -a  $str1 = $str2
echo $?

echo -n "Is a and b are equal, and str1 and str2 are equal? "
test $a -eq $b -a  $str1 = $str2
echo $?

echo -n "Does /tmp is a sirectory and execute permission exists? "
test -d /tmp -a  -x /tmp
echo $?

echo -n "Is /tmp file is a block file or write permission exists? "
test -b /tmp -o -w /tmp
echo $?

The output of running this script is as follows:

a = 5 , b = 56 , str1 = Hello , str2 = Hello
Is a and b are not equal, and str1 and str2 are equal? 0
Is a and b are equal, and str1 and str2 are equal? 1
Does /tmp is a sirectory and execute permission exists? 0
Is /tmp file is a block file or write permission exists? 0

Similar to other checks with the test command, the 0 exit code means the expression evaluated is true and 1 means false evaluation.

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

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