The test command in the Shell is used to check if a condition is true, and it can be tested in three aspects of numeric, character, and file.
Numerical test
Parameter description
-eq equals True
-ne is not equal to true
-GT is greater than true
-ge is true if it is greater than or equal
-lt is less than true
-le is less than or equal to true
Example Demo:
num1=100
num2=100
if test $[num1]-eq $[num2]
Then
Echo ' Two numbers equal! ‘
Else
Echo ' Two numbers not equal '
Fi
Output Result:
Two numbers equal!
The [] in the code performs basic arithmetic operations such as:
#!/bin/bash
A=5
B=6
RESULT=$[A+B] # Note that there must be no spaces on either side
echo "Result: $result"
The result is:
Result is: 11
String test
Parameter description
= Equals is True
! = True if not equal
-Z String string is true if the length is zero
-N String string is true if the length is nonzero
Example Demo:
num1= "Rulnoob"
Num2= "Runoob"
if test $num 1 = $num 2
Then
Echo ' Two strings equal! '
Else
Echo ' two strings are not equal! '
Fi
Output Result:
Two strings are not equal!
File test
Parameter description
-e filename stating that the file exists is true
-r file name True if the file exists and is readable
-W file name True if the file exists and is writable
-X file name if file exists and executable is true
-d file name if the file exists and is true for the directory
-f filename If the file exists and is a character special file is True
-B file name True if file exists and is a block special file
Example Demo:
Cd/bin
If Test-e./bash
Then
Echo ' file already exists! '
Else
Echo ' file does not exist! '
Fi
Output Result:
File already exists!
In addition the Shell is provided with (-a) or (-O) non (! Three logical operators are used to link test conditions, with the priority being:! Highest-a second-o lowest
Shell Test Command