difference in the two test operators


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting difference in the two test operators
# 1  
Old 01-06-2012
difference in the two test operators

could any one please help me understand the difference between double square bracket test operator and single square bracket test operator.

I have seen one difference with the below code but not sure which one has to be used and when:

Code:
 
unix79:z54402:/data/ds/scripts $ cat > T
one,,two
unix79:z54402:/data/ds/scripts $ z=`cat T|cut -d, -f2,2`
unix79:z54402:/data/ds/scripts $ if [[ -z $z ]]
> then echo success
> else echo fail
> fi
success
unix79:z54402:/data/ds/scripts $ if [ -z $z ]
> then echo success
> else echo fail
>  fi
ksh: test: 0403-004 Specify a parameter with this command.
fail
unix79:z54402:/data/ds/scripts $

# 2  
Old 01-06-2012
# 3  
Old 01-06-2012
Many thanks..... sorry for the blind post...... Just entered here Smilie
# 4  
Old 01-06-2012
This is described in the "man" pages for Shells such as the Posix Shell and Korn Shell.
The single bracket version [ ] is a shorthand version of the unix "test" command and is described in outline terms in the Shell manual and in detail in "man test".
The double bracket version [[ ]] is a Conditional Expression and is described in that section of the Shell manual.
There is much overlap in basic syntax between the two. They have totally different syntax for boolean "and/or" conditions.

In your examples, both of them need mandatory double quotes round the string variable $z. Only the second example gave a syntax error, but both were wrong.

Code:
z=`cat T|cut -d, -f2,2`
if [[ -z "$z" ]]
then
       echo success
else
       echo fail
fi


if [ -z "$z" ]
then
       echo success
else
       echo fail
fi

Beware that the "cut" will misbehave if there is one field in the data file but no commas. Try it.

Always put string variables in double quotes and you will have much less trouble with syntax.
Login or Register to Ask a Question

Previous Thread | Next Thread

8 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Bash -o -v -R operators

I do not know the use of the -o -v -R operators. This is what the info says and I am confused of what optname and varname mean, are they just normal variable? -o optname True if the shell option optname is enabled. See the list of options under the ... (6 Replies)
Discussion started by: kristinu
6 Replies

2. Homework & Coursework Questions

Operators

I really don't know the meaning of these operators. Could someone explain the meanings? <, <=, ==, !=, >=, >, ||, &&, ! ~ , !~ Thanks! (1 Reply)
Discussion started by: Erjen
1 Replies

3. Shell Programming and Scripting

Operators

I really don't know the meaning of these operators. Could someone explain the meanings so I can make my test for today? <, <=, ==, !=, >=, >, ||, &&, ! ~ , !~ Thanks! (1 Reply)
Discussion started by: Erjen
1 Replies

4. Shell Programming and Scripting

How to check weather a string is like test* or test* ot *test* in if condition

How to check weather a string is like test* or test* ot *test* in if condition (5 Replies)
Discussion started by: johnjerome
5 Replies

5. Shell Programming and Scripting

And and OR Operators with If Statement.

Hi All, I have 2 variables. Result1 and Result2. I want to put a condition that if Both are True then echo "All True" Else Show Error. Right now i am doing this and getting error. if ; then echo "All True" else echo "Failed" fi; Error. line 8: ' Solution: Looking for (2 Replies)
Discussion started by: mkashif
2 Replies

6. Shell Programming and Scripting

Test on string containing spacewhile test 1 -eq 1 do read a $a if test $a = quitC then break fi d

This is the code: while test 1 -eq 1 do read a $a if test $a = stop then break fi done I read a command on every loop an execute it. I check if the string equals the word stop to end the loop,but it say that I gave too many arguments to test. For example echo hello. Now the... (1 Reply)
Discussion started by: Max89
1 Replies

7. UNIX for Dummies Questions & Answers

Operators

I am trying to understand Does the following: {tmp+=$10} Mean take $10 and add them all up and call it tmp thanks! (2 Replies)
Discussion started by: llsmr777
2 Replies

8. UNIX for Advanced & Expert Users

If with set operators

Hi guys, I'm trying to run more than one "if" condition at once. What I want is something like if ] or ] or ]; then ... I can't remember the syntax for using this or/and set operators. Can someone please assist/ jog my memory? thanks Khoom (2 Replies)
Discussion started by: Khoomfire
2 Replies
Login or Register to Ask a Question