The UNIX and Linux Forums  

Go Back   The UNIX and Linux Forums > Top Forums > UNIX for Dummies Questions & Answers
Google UNIX.COM


UNIX for Dummies Questions & Answers If you're not sure where to post a UNIX or Linux question, post it here. All UNIX and Linux newbies welcome !!

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
Conditonal clause in expect. akbar Shell Programming and Scripting 1 05-13-2008 04:28 PM
if clause palmer18 UNIX for Dummies Questions & Answers 3 08-08-2007 06:22 AM
if clause in AWK END block not working. satnamx Shell Programming and Scripting 1 04-21-2006 03:29 AM
Math Square Root davex4285 Shell Programming and Scripting 3 01-29-2006 07:03 PM
square brackets gilead29 UNIX for Dummies Questions & Answers 2 02-03-2004 08:29 AM

Reply
 
Submit Tools LinkBack Thread Tools Display Modes
  #1  
Old 11-15-2006
Registered User
 

Join Date: Oct 2006
Location: Bangalore, India
Posts: 41
why put double square brackets in an if clause?

what is the rationale behind putting double square brackets in an if clause?

for e.g.

if [[ ${APPNAME} = '' ]] || [[ ${SRCSYSNAME} = '' ]] || [[ ${FILENAME} = '' ]];
then
echo some fields are null
fi
Reply With Quote
Forum Sponsor
  #2  
Old 11-15-2006
Registered User
 

Join Date: Aug 2006
Posts: 122
What shell is it running under?
What UNIX do you have?
Reply With Quote
  #3  
Old 11-15-2006
Perderabo's Avatar
Unix Daemon
 

Join Date: Aug 2001
Location: Washington DC Area
Posts: 8,610
See this post
Reply With Quote
  #4  
Old 11-15-2006
Registered User
 

Join Date: Oct 2006
Location: Bangalore, India
Posts: 41
The following is a segment from that post.
"There is a command called [ and like many commands it takes parameters. It checks its last parameter and gets mad unless it is ]. If the last parameter is a ], it looks at everything else and tries to determine what you are trying to test. If you do:
lines=""
if [ $lines -eq 0 ]
you will have a problem. All that the [ command will see is the -eq and the 0 so it will complain. Because of these concerns, Dave Korn invented another syntax. Now you can do:
if [[ $lines = 0 ]]
and it won't get mixed up as easily."

'All that the [ command will see is the -eq and the 0'...why so? why wont it see the ']' ? isnt ']' the last argument for command '[' ? plz explain. and by 'it looks at everything else' , u mean that it looks inside the square brackets? if so, then i dont see, why the first format shudnt work. (it isnt working, obviously, but i cant get the reason)
Reply With Quote
  #5  
Old 11-15-2006
Registered User
 

Join Date: Oct 2006
Location: Bangalore, India
Posts: 41
i am using the bash shell by the way. solaris
Reply With Quote
  #6  
Old 11-16-2006
Perderabo's Avatar
Unix Daemon
 

Join Date: Aug 2001
Location: Washington DC Area
Posts: 8,610
I carelessly phrased that. Yes it sees the ] but the $lines just disappears. It starts out:
if [ $lines -eq 0 ]
The shell replaces $lines with nothing. Now we have:
if [ -eq 0 ]
So yes the ] is there but the test no longer makes sense. This cannot happen with the [[ style.
Reply With Quote
Google The UNIX and Linux Forums
Reply

Thread Tools
Display Modes




All times are GMT -7. The time now is 04:07 AM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited.
The UNIX and Linux Forums Content Copyright ©1993-2008. All Rights Reserved.Ad Management by RedTyger Visit The Complex Event Processing Blog

Content Relevant URLs by vBSEO 3.2.0