In the last article we talked about the basics of Linux shell scripts. In this one, we’ll mention test
, a command-line utility that evaluates and compares conditional expressions. Test
is a shell builtin most commonly used with the if
statement. Depending on the expression, conditional statement could be unary or binary. For example, -e filepath
is an unary command while $VAR1 < $VAR2
is a binary command. Test
has two variations, which are equivalent, apart from the syntax:
test expression
and
[ expression ]
Keep in mind that the second variation (with the brackets) needs to have blank spaces before and after the expression.
As mentioned in the previous article, test
command has a termination or exit status, depending on the computed expression. The value of 0 indicates the success of the expression, and the value of 1 indicates the failure of the expression.
String expressions
Test
provides the following expressions in order to evaluate strings:
Expression | Evaluates: |
---|---|
string | string is not null |
-n string | String length is > 0 |
-z string | String length is = 0 |
string1 = string2 | string1 and string2 are equal |
string1 == string2 | string1 and string 2 are equal |
string1 != string2 | string1 and string2 are not equal |
string1 > string2 | string1 sorts after string2 |
string1 < string2 | string1 sorts before string2 |
echo "Hello, what is your name?" read NAME if [ -z "$NAME" ]; then echo "The name is empty!" exit 1 fi if [ -n "$NAME" ]; then echo "Hello! ${NAME}" if [ "$NAME" = "Tom" ]; then echo "Oh it is you Tom!" else echo "I didn't expect you!" fi fi
Above we have a simple example that uses some of the string expressions in the table. We’re evaluating the variable NAME
. We first check whether the string exists. If not, we echo the error message and terminate the script. By echoing $?
we can see the exit status which is 1.
Otherwise, if we have the name, then we’ll issue a greeting, and we’ll have another check in which we compare our name to Tom using an if/else
condition.
Integer expressions
Expressions that are used with integers are as follows:
Expression | Evaluates: |
---|---|
num1 -eq num2 | num1 is equal to num2 |
num1 -ne num2 | num1 is not equal to num2 |
num1 -le num2 | num1 is less than or equal than num2 |
num1 -lt num2 | num1 is less than num2 |
num1 -ge num2 | num1 is greater than or equal than num2 |
num1 -gt num2 | num1 is greater than num2 |
echo "Please enter your number" read NUM if [ -z "$NUM" ]; then echo "The number is empty!." exit 1 fi if [ $NUM -gt 0 ]; then echo "The num ${NUM} is greater than zero" else echo "The num ${NUM} is less than zero or zero" fi if [ $((NUM % 2)) -eq 0 ]; then echo "The num ${NUM} is even." else echo "The num ${NUM} is odd." fi
As with the string expression we test a couple of simple scenarios. Whether the number exists, whether the number is greater than zero and whether the number is even or odd (using the modulo operator like in most other programming languages).
File expressions
Expressions that are used with files are the following:
Expression | Evaluates: |
---|---|
file1 -ef file2 | file1 and file2 refer to the same device and inode number |
file1 -nt file2 | file1 is newer than file2 |
file1 -ot file2 | file1 is older than file2 |
-a file | file exists |
-e file | file exists |
-d file | file exists and is a directory |
-f file | file exists and is a regular file |
-g file | file exists and is set-group-ID |
-G file | file exists and is owned by the group ID |
-L file | file exists and is symbolic link |
-p file | file exists and is named pipe (FIFO) |
-r file | file exists and is readable |
-s file | file exists and has a length greater than zero |
-S file | file exists and is a network socket |
-u file | file exists and is setuid |
-w file | file exists and is writable |
-x file | file exists and is executable |
FILE=/home/mehmed/Projects/shell/hello if [ -e "$FILE" ]; then echo "File $FILE exists." if [ -d $FILE ]; then echo "File $FILE is a directory". fi if [ -f $FILE ]; then echo "File $FILE is a regular file". fi if [ -s $FILE ]; then echo "$FILE has content". fi if [ -w $FILE ]; then echo "$FILE is writeable". fi if [ -r $FILE ]; then echo "$FILE is readable". fi if [ -x $FILE ]; then echo "$FILE is executable". fi else echo "File $FILE does not exist." exit 1 fi
Above we’re checking first for the existence of the file and then, as the script goes on whether the file is a directory, a regular file, whether it has content, or whether it is writeable, readable, and executable. If the file doesn’t exist, we echo the message, and exit with the status code of 1.