UNIX Help
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me
Go Back   Dev Shed ForumsOperating SystemsUNIX Help

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
Stop making mediocre tutorials.The best tutorials are video! Camtasia Studio makes it easy to create engaging, buzz-building screen videos at any size, in any popular format. Download the free trial!
  #1  
Old September 28th, 2004, 10:10 AM
AVS AVS is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Aug 2004
Posts: 37 AVS User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 1 h 4 m 52 sec
Reputation Power: 4
Difference between if test and if [[ w.r.t shells

Hi,

I am trying to understand teh difference between the following

if test $a -le 20
then
print $a
else
print big
fi

and

if [[ $a -le 20 ]];
then
print $a
else
print big
fi

I put them in 2 different files named it aaa and bbb. Both of them gave same results when run in ksh or bash or csh. No errrors.
May be it is because of the to first line I put in both the shell scripts which is
#! /bin/ksh

When I removed this line from aaa and bbb then also it was giving me same results except for one case
bash does not recognize 'print' when there is no #! /bin/ksh at the top

Any more thoughts on this

Thanks

Reply With Quote
  #2  
Old September 28th, 2004, 10:27 AM
Perderabo Perderabo is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Nov 2003
Posts: 121 Perderabo User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 9 m 54 sec
Reputation Power: 5
Short answer: put a line like
a=""
at the top and one difference will become visible.

This show the evolution of the shell. At first,
if test $a -le 20
was all there was. And test was an external program sitting in /bin/test. The next step was to give test a second name called [. And there really was a /bin/[ to support it. The [ expected that it's last argument was ]. Some implementations of test would check for this, but usually they just ignore the last argument. So now syntax like:
if [ $a -le 20 ]
became possible.

The next step was to make [ a shell built-in command. This standardized how it worked and made it much faster.

But there is a problem:
a=""
if [ $a -le 20 ]
Here $a will disappear before the test command sees its arguments. To the test command it looks like:
if [ -le 20 ]
which doesn't make sense. There is a solution, put double quotes around the $a like this "$a".

When Dave Korn wrote the korn shell, he wanted to keep the [ syntax the same for compatability with existing scripts. But he wanted to solve the above problem. And he wanted to add a lot of stuff. So he invented the new
if [[ ... ]]
syntax. Bash and some other shells picked it up from ksh.

Reply With Quote
  #3  
Old September 28th, 2004, 12:05 PM
andyb1ack andyb1ack is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Sep 2004
Posts: 60 andyb1ack User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 19 m 14 sec
Reputation Power: 4
Nice post Perderabo. Very informative.

Reply With Quote
Reply

Viewing: Dev Shed ForumsOperating SystemsUNIX Help > Difference between if test and if [[ w.r.t shells


Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump


Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 





© 2003-2008 by Developer Shed. All rights reserved. DS Cluster 2 hosted by Hostway