Skip to content

Linux test command (test conditions)

The test command is a great tool for testing conditions. Based on the return variable of the test command, programming logics can be set up to execute something if a condition is satisfied or not.

It can test conditions on files, permissions, texts, numbers, and even through expressions and provide the result through the return variable $? .

In this example, the test command checks that 1 is less than 2 (-lt = less than - less than):

$ test 1 -lt 2
$ echo $?
0

Since the return variable was 0, the number 2 is less than 1.

In this other example, it tests with the “-d” if the file /usr/src is a directory:

$ test —d /usr/src <br></br>$ echo $?

0

The most common test command options are:

**Option****What Is It****For Example**
-d /directoryChecks if a file is a directory and that it exists$ test -d /etc
-e fileChecks if a file exists regardless of its type$ test -e /etc/passwd
-f fileChecks whether a file exists and whether it is common$ test -f /etc/passwd
-z fileCheck if the file is empty$ test -z /etc/groups
-n “$ variable”Check if the size of a text is greater than zero$ test -n “$BOOK”
-s fileChecks if a file exists and that its size is greater than zero$ test -s /etc/passwd
s1 = s2Checks if a text is the same as another$ test “$BOOK” = “Linux Certification”
s1! = s2Check if a text is different from another$ test “$BOOK”! = “Linux Certification”
x -eq yChecks if a number **x** equals **y**$ VAR=10; test $VAR -eq 10
x -gt yCheck if a number **x** is greater than **y**$ VAR=10; test $ VAR -gt 9
x -lt yCheck if a number **x** is smaller than **y**$ VAR=10; test $VAR -lt 11
x -ne yChecks if a number **x** is not equal to **y**$ VAR=10; test $VAR -ne 11
x -ge yCheck if a number **x** is greater than or equal to **y**$ VAR=10; test $VAR -ge 9
x -le yCheck if a number **x** is less than or equal to **y**$ VAR=10; test $VAR -le
expression -a expressionCheck if both expressions are true$ test 10 -ge 9 -a 22 -gt 10; echo $?
expression -o expressionCheck if an OR expression are true$ test 10 -ge 11 -o 22 -gt 10; echo $?
file1 -nt file2Checks if file1 was modified after file2 (newer)$ test file1 -nt file2
file1 -ot file2Checks if file1 was modified before file2 (older)$ text file1 -ot file2
-r fileChecks if the file is allowed to read$ test -r file
-w fileChecks if the file has writing permission$ test -w file
-x fileChecks if the file has permission to execute$ test -x file
-The fileChecks whether you are the owner of the file$ test -The file

Use of [ ] instead of “test”

The test command can be abbreviated with brackets []. Note that there must be a space between the expression and the brackets:

$ [10 -ge 8]
$ echo $?
0

|| and && operators

The operators (pipe pipe) II and && are used to verify whether the expression on the right returns true or false.

The && operator checks whether something on your right is true, and therefore executes the command on your left.

In this example, we force return true with the “true” command, which will always return true:

$ true && echo “It worked” It worked

If we use the “false” command, which always returns false, the && operator will not execute the command on the left:

$ false && echo “It worked”

Operator II checks if something on your right is false, and therefore executes the command on your left.

In this example, we force return true with the true command. In this case, the II will not execute anything.

$ true || echo “It worked”

By the way, forcing a false return with the false command, II executes the echo command:

$ false || echo “It worked” It worked

The && and II operators can also be used together:

$ test 100 -gt 99 && echo “YES” || echo “NO”

In this case it will return YES because 100 is greater than 99.

Flow control with if (se)

Flow controls (if) are commands that test some alternatives and, according to the result, execute commands.

The logic of the if command is “if this is true, do this, if not, do that”. See the example:

if [$? = 0]; then
echo 'The command completed successfully... '
else
echo 'The command failed. '
fi

In this example the if tests whether the content of the return variable $? It’s 0. If so, execute the echo command “The command completed successfully…”. If the condition is false, execute the command echo “The Command failed.”.

Unlike other languages, if tests a command and not a condition. Therefore, we must use the test command together with the if command. It is generally written using the shortcut of brackets [ ] for the test command.

Flow control with case

The case is an intelligent way to compare the value of something with various standards.

If an occurrence is positive, it allows commands to be executed.

Each case is an expression that corresponds to a pattern.

The ”)” operator ends a list of patterns and starts a list of commands.

What separates one pattern from another is ”;;”.

At the end of the case, you must finish with the esac instruction (case on the contrary).

Example of a script that, depending on the user’s UID, prints a different message:

#! /bin/bash
ID=$(id -u)
case “$ID” in
0)
echo “You are the root.” ;
echo “Congratulations!”
;;
1000)
echo “You are Uira.”
;;
100[1-9])
echo “You are another user”
esac
echo “this is the end”;

If the value of the ID variable is 0, it will print a congratulations message. If it is equal to 1000, print “you are Uira”. If it’s between 1001 and 1009, print “You are another user.

If executed with the user uira, who has a UID equal to 1000:

$ ./meucase
You are Uira.
This is the end

If run as root:

$ sudo ./mycase
You are root.
Congratulations!
This is the end

The case is better than using the “if” command several times to compare something with various patterns.

Loops with the for command

$ for something; do command; command2; done

The shell has structures for testing conditions and executing certain program sequences several times (loop), until the tested condition is satisfied.

The three commands that enable this are for (until some condition), while (as long as it is true) and until (as long as it is false).

The for command allows loops to be made so that one or more commands are executed until a given variable goes through all the values in a given list.

Here’s a simple example:

$ for number in one two three four five
do
echo “Counting: $number”
done
Counting: one
Counting: two
Counting: three
Counting: four
Counting: five

The same command can be written on a single line:

$ for number in one two three four five; do echo “Counting: $number”; done

See another example using a list in a file:

$ ls -1 > list_files.txt

You can now use for to execute several commands for each filename in the list list_files.txt:

$ for i in $(cat list_files.txt); do cp $i $i.backup; mv $i.backup /tmp/backup; done;

This loop assigns each line of the list_files.txt file to the $i variable.

The expression $(command) generates a list with the result of the command. It has the same effect as using the command between classes: **command**.

It then executes the commands to rename and move the files until the list is complete.

Now a slightly more complex example, using for with if:

$ for i in $(ls -1); do if test -f $i; then echo “$i is a file“; else echo “$i is not a file”; fi; done
Desktop is not a File
Documents is not a File
Downloads is not a File
HR_PROFESSOR.sql is a file

In this line, the result of the ls -1 command is placed in the variable $i; then each line of the variable $i is tested to find out if it is a file; if true, a sentence will be displayed and, if false, another sentence will be displayed.

Loops with the while command

$ while [expression]; of the command; command; done

While continuously tests an expression until an expression has a return variable equal to zero or true.

In this example, the variable $i gains an initial value equal to zero.

As long as (while) the test command condition represented by [ ] returns zero (true), the loop will continue to execute.

In the loop, the variable $i is increased until $i reaches the condition of not being less than 4:

$ i=0
$ while [$i -lt 4]; do echo “account $i”; i=$[$i+1]; done

account 0
account 1
account 2
account 3

The while must have a satisfied exit condition, otherwise it will be in an infinite loop.

Loops with the until command

The until command is the opposite of the while command, but it executes something while the test is false, or with non-zero values.

Its syntax is:

until [CONDITION] of [COMMANDS] done

Example:

#! /bin/bash

counter=0
until [$counter -gt 3]
do
echo Counter: $counter
(counter++))
done

When executed:

$ ./counter

Counter: 0
Counter: 1
Counter: 2
Counter: 3

Until is useful for making a script wait until a command no longer returns an error, since it is only executed if the test return is different from zero.

A program’s zero exit code generally indicates that it ran successfully.

In this example, the until command is used to wait until the host is able to ping IP 8.8.8.8. When the ping command succeeds in pinging host 8.8.8.8, it will return zero, causing the processing to leave the loop:

#! /bin/bash
until ping -c 1 8.8.8.8 &>/dev/null
do
echo “Waiting for the network...”
sleep 1
done
echo “The network is on the air”