Unix Shell - bash : what is wrong of this if command

This is Interesting: Free IT Magazines  
Home > Archive > Unix Shell > March 2005 > bash : what is wrong of this if command





You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

Author bash : what is wrong of this if command
clinton__bill@hotmail.com

2005-03-30, 7:55 am

Hi,
I just come to bash from csh, there is one problem in using if
command:

bash-2.05b$ if [ `uname`=="SunOS" ]; then echo haha; fi
haha
bash-2.05b$ if [ `uname`!="SunOS" ]; then echo haha; fi
haha

Do you see the problem of the above 2 if command. If I run "uname", I
see "Linux" returns.

Thanks.

Andre Majorel

2005-03-30, 7:55 am

On 2005-03-25, Chris F.A. Johnson <cfajohnson@gmail.com> wrote:
> On Fri, 25 Mar 2005 at 17:08 GMT, clinton__bill@hotmail.com wrote:
>
> You need spaces around the operator:
>
> if [ "`uname`" = "SunOS" ]; then echo haha; fi
>
> if [ "`uname`" != "SunOS" ]; then echo haha; fi


And, as Chris did, use "=" for equality. "==" is not portable.

--
André Majorel <URL:http://www.teaser.fr/~amajorel/>
(Counterfeit: qipiriquh@gasohol.com zydeheh@dissuade.com)
What worries me is not the violence of the few, but the
indifference of the many. -- M. L. King
William Park

2005-03-30, 7:55 am

clinton__bill@hotmail.com wrote:
> Hi,
> I just come to bash from csh, there is one problem in using if
> command:
>
> bash-2.05b$ if [ `uname`=="SunOS" ]; then echo haha; fi
> haha
> bash-2.05b$ if [ `uname`!="SunOS" ]; then echo haha; fi
> haha
>
> Do you see the problem of the above 2 if command. If I run "uname", I
> see "Linux" returns.


You need space around '=' and '!=', like
if [ `uname` = "SunOS" ]; ...
if [ `uname` != "SunOS" ]; ...

--
William Park <opengeometry@yahoo.ca>, Toronto, Canada
Slackware Linux -- because it works.

Chris F.A. Johnson

2005-03-30, 8:47 pm

On Fri, 25 Mar 2005 at 17:08 GMT, clinton__bill@hotmail.com wrote:
> Hi,
> I just come to bash from csh, there is one problem in using if
> command:
>
> bash-2.05b$ if [ `uname`=="SunOS" ]; then echo haha; fi
> haha
> bash-2.05b$ if [ `uname`!="SunOS" ]; then echo haha; fi
> haha
>
> Do you see the problem of the above 2 if command. If I run "uname", I
> see "Linux" returns.


You need spaces around the operator:

if [ "`uname`" = "SunOS" ]; then echo haha; fi

if [ "`uname`" != "SunOS" ]; then echo haha; fi

--
Chris F.A. Johnson http://cfaj.freeshell.org/shell
========================================
===========================
My code (if any) in this post is copyright 2005, Chris F.A. Johnson
and may be copied under the terms of the GNU General Public License
Sponsored Links






Free braindumps | Software forum | Database administration forum

Copyright 2003 - 2008 webservertalk.com