test command or [ expr ] in Linux Shell Script

By: Dorris Emailed: 1586 times Printed: 2037 times    

Latest comments
By: rohit kumar - how this program is work
By: Kirti - Hi..thx for the hadoop in
By: Spijker - I have altered the code a
By: ali mohammed - why we use the java in ne
By: ali mohammed - why we use the java in ne
By: mizhelle - when I exported the data
By: raul - no output as well, i'm ge
By: Rajesh - thanx very much...
By: Suindu De - Suppose we are executing

test command or [ expr ] is used to see if an expression is true, and if it is true it return zero(0), otherwise returns nonzero for false.
Syntax: 
test expression OR [ expression ]

Example:
Following script determine whether given argument number is positive.

$ cat > ispostive
#!/bin/sh
#
# Script to see whether argument is positive
#
if test $1 -gt 0
then
echo "$1 number is positive"
fi

Run it as follows
$ chmod 755 ispostive

$ ispostive 5
5 number is positive

$ispostive -45
Nothing is printed

$ispostive
./ispostive: test: -gt: unary operator expected

Detailed explanation
The line, if test $1 -gt 0 , test to see if first command line argument($1) is greater than 0. If it is true(0) then test will return 0 and output will printed as 5 number is positive but for -45 argument there is no output because our condition is not true(0) (no -45 is not greater than 0) hence echo statement is skipped. And for last statement we have not supplied any argument hence error ./ispostive: test: -gt: unary operator expected, is generated by shell , to avoid such error we can test whether command line argument is supplied or not.

test or [ expr ] works with
1.Integer ( Number without decimal point)
2.File types
3.Character strings

For Mathematics, use following operator in Shell Script

 
Mathematical Operator in  Shell Script  Meaning Normal Arithmetical/ Mathematical Statements But in Shell
      For test statement with if command For [ expr ] statement with if command
-eq is equal to 5 == 6 if test 5 -eq 6 if [ 5 -eq 6 ]
-ne is not equal to 5 != 6 if test 5 -ne 6 if [ 5 -ne 6 ]
-lt is less than 5 < 6 if test 5 -lt 6 if [ 5 -lt 6 ]
-le is less than or equal to 5 <= 6 if test 5 -le 6 if [ 5 -le 6 ]
-gt is greater than 5 > 6 if test 5 -gt 6 if [ 5 -gt 6 ]
-ge is greater than or equal to 5 >= 6 if test 5 -ge 6 if [ 5 -ge 6 ]

NOTE: == is equal, != is not equal.

For string Comparisons use

Operator Meaning
string1 = string2 string1 is equal to string2
string1 != string2 string1 is NOT equal to string2
string1 string1 is NOT NULL or not defined 
-n string1 string1 is NOT NULL and does exist
-z string1 string1 is NULL and does exist

Shell also test for file and directory types

Test Meaning
-s file    Non empty file
-f file    Is File exist or normal file and not a directory 
-d dir     Is Directory exist and not a file
-w file   Is writeable file
-r file    Is read-only file
-x file    Is file is executable

Logical Operators

Logical operators are used to combine two or more condition at a time

Operator            Meaning
! expression Logical NOT
expression1  -a  expression2 Logical AND
expression1  -o  expression2 Logical OR

Linux Home | All Linux Tutorials | Latest Linux Tutorials

Sponsored Links

If this tutorial doesn't answer your question, or you have a specific question, just ask an expert here. Post your question to get a direct answer.



Bookmark and Share

Comments(0)


Be the first one to add a comment

Your name (required):


Your email(required, will not be shown to the public):


Your sites URL (optional):


Your comments:



More Tutorials by Dorris
sum of all digits in a given number using Shell Script
sh -x and sh -v in Linux
case..esac..in Linux Shell Script
while loop in Linux Shell Script
Nested for loop in Linux Shell Script
for loop in Linux Shell Script
Multilevel if-then-else in Linux Shell Script
Nested if-else-fi in Linux Shell Script
if...else...fi in Linux Shell Script
test command or [ expr ] in Linux Shell Script
if condition in Linux Shell Script
Download Linux. Which linux and where?
Formula in Hibernate
Primary keys assigned by triggers in Hibernate
Assigned identifiers in Hibernate

More Tutorials in Linux
smskannel SMS gateway run in background
Running jar files in background in ssh window
Could not open '': No such file or directory at lib/ExtUtils/MM_Unix.pm line 2697
Can't locate ExtUtils/MakeMaker.pm in @INC ...
bash: make: command not found
apxs: command not found
bash: svn: command not found
make: Nothing to be done for `all'.
How to burn your CD / DVD ISO image using Media Creator (Adaptec/Roxio) on Windows
How to burn your CD / DVD ISO image using Nero Burning ROM (Ahead Software) on Windows
How to burn your CD / DVD ISO image using Nero Express (Ahead Software) on Windows
How to burn your CD / DVD ISO image using NISO Recorder V2 Power Toy on Windows
How to burn your CD / DVD ISO image using k3b on CentOS
Compiling and Installing software from source in Linux
Installing RPM's using the Redhat Package Manager in Linux

More Latest News
Most Viewed Articles (in Linux )
Nested if-else-fi in Linux Shell Script
bash: svn: command not found
Sort numbers using arrays in Shell Script
Rules for Naming variable names in Linux Shell Script
How to print or access value of user defined variables in Linux Shell Script
test command or [ expr ] in Linux Shell Script
make: Nothing to be done for `all'.
Could not open '': No such file or directory at lib/ExtUtils/MM_Unix.pm line 2697
Can't locate ExtUtils/MakeMaker.pm in @INC ...
echo Command in Linux Shell Script
Multilevel if-then-else in Linux Shell Script
sh -x and sh -v in Linux
Getting System Information in Shell Script
Pipes in Linux
for loop in Linux Shell Script
Most Emailed Articles (in Linux)
How to print or access value of user defined variables in Linux Shell Script
if condition in Linux Shell Script
Rules for Naming variable names in Linux Shell Script
Formating text in Shell Script
How to burn your CD / DVD ISO image using NISO Recorder V2 Power Toy on Windows
How to use Linux?
How to write your first Shell Script in linux?
echo Command in Linux Shell Script
Filter in Linux
if...else...fi in Linux Shell Script
sum of all digits in a given number using Shell Script
Command line arguments in Shell Script
Menu based programs in Shell Script
How to burn your CD / DVD ISO image using Nero Burning ROM (Ahead Software) on Windows
Could not open '': No such file or directory at lib/ExtUtils/MM_Unix.pm line 2697