Test Command (integral part of the conditional statements)

  •  the test command compares one element against another and returns true or false. In bash scripting, 
  • the test command is an integral part of the conditional statements


The test command takes an EXPRESSION as an argument. 
After calculating the EXPRESSION, the test returns a value to the bash variable “$?”. 
  • If the value is 0, then the expression evaluation was true.
  • If the value is 1, then the expression evaluation was false.


syntax's:

  • test EXPRESSION
  • [ EXPRESSION ]


----------------------------------------------------------------------------------------------------------------------------------
Example
----------------------------------------------------------------------------------------------------------------------------------
[haritaraka12078@cxln4 ~]$ cat>test1.sh
$ test 1 -eq 2 && echo “true” || echo “false”

[haritaraka12078@cxln4 ~]$ sh test1.sh
test1.sh: line 1: $: command not found
“false”

  • test: The test command.
  • 1: The first element for comparison.
  • -eq: Comparison method (whether values are equal).
  • 2: The second element for comparison.


----------------------------------------------------------------------------------------------------------------------------------
Example:  same command can be expressed using “[“
----------------------------------------------------------------------------------------------------------------------------------

[haritaraka12078@cxln4 ~]$ cat>test2.sh
$ [ 1 -eq 2 ] && echo “true” || echo “false”


[haritaraka12078@cxln4 ~]$ sh test2.sh
test2.sh: line 1: $: command not found
“false”



----------------------------------------------------------------------------------------------------------------------------------
Example: 
----------------------------------------------------------------------------------------------------------------------------------

[haritaraka12078@cxln4 ~]$ cat>test3.sh

To check the condition
echo "enter number:"
read a
if test $a -gt 0
then
echo "$a is :positive"
else
echo "$a is nagative"
fi

[haritaraka12078@cxln4 ~]$ sh test3.sh
test3.sh: line 1: To: command not found
enter number:
2
2 is :positive


[haritaraka12078@cxln4 ~]$ sh test3.sh
test3.sh: line 1: To: command not found
enter number:
0
0 is nagative



File Test commands
----------------------

-s file : True if the file exists and has a size greater than zero
-f file : True if the file exists and is not a directory
-d file : True if the file exists and is  directory file
-c file : True if the file exists and is a character special file
-b file : True if the file exists and is a block special file
-r file : True if the file exists and You have a read permision to it
-w file : True if the file exists and You have a read write to it
-x file : True if the file exists and You have a read executiion to it


----------------------------------------------------------------------------------------------------------------------------------
Example: 
----------------------------------------------------------------------------------------------------------------------------------
[haritaraka12078@cxln4 ~]$ cat test4.sh
echo "direcory Name:"
read dir
if test -d $dir or if[ -d $dir ]
then
echo " $dir directory is a valid directory"
else
echo "Entered directory is not  a valid directory"
fi


[haritaraka12078@cxln4 ~]$ sh test4.sh
direcory Name:
hari_dir
test4.sh: line 3: test: too many arguments
Entered directory is not  a valid directory



----------------------------------------------------------------------------------------------------------------------------------
Example: 
----------------------------------------------------------------------------------------------------------------------------------
[haritaraka12078@cxln4 ~]$ cat>test11.sh
num=4; if (test $num -gt 5); then echo "yes"; else echo "no"; fi
num=6; if (test $num -gt 5); then echo "yes"; else echo "no"; fi
[haritaraka12078@cxln4 ~]$ chmod 777 test11.sh
[haritaraka12078@cxln4 ~]$ sh test11.sh
no
yes



----------------------------------------------------------------------------------------------------------------------------------
STRING Test Commands
----------------------------------------------------------------------------------------------------------------------------------


  • -n <string>: String length is non-zero.(True if the length of string is greatr than 0)
  • -z <string>: String length is zero.(True if the length of the string is zero)

<string>: String value is non-zero (quivalent to “-n <string>”).
<string_a>= <string_b>: Both string_a and string_b are equal.
<string_a> != <string_b>: The strings string_a and string_b aren’t equal.




Example:

[haritaraka12078@cxln4 ~]$ cat>test5.sh
echo "enter string"
read string
if [ -z $string ]
then
echo "you Entered  Invalid string"
else
echo "valid string"
fi


[haritaraka12078@cxln4 ~]$ chmod 777 test5.sh

[haritaraka12078@cxln4 ~]$ sh test5.sh
enter string
prabhu
valid string


[haritaraka12078@cxln4 ~]$ sh test5.sh
enter string

you Entered  Invalid string