delorie.com/archives/browse.cgi   search  
Mail Archives: cygwin/2002/10/02/10:18:18

Mailing-List: contact cygwin-help AT cygwin DOT com; run by ezmlm
List-Subscribe: <mailto:cygwin-subscribe AT cygwin DOT com>
List-Archive: <http://sources.redhat.com/ml/cygwin/>
List-Post: <mailto:cygwin AT cygwin DOT com>
List-Help: <mailto:cygwin-help AT cygwin DOT com>, <http://sources.redhat.com/ml/#faqs>
Sender: cygwin-owner AT cygwin DOT com
Mail-Followup-To: cygwin AT cygwin DOT com
Delivered-To: mailing list cygwin AT cygwin DOT com
X-Authentication-Warning: slinky.cs.nyu.edu: pechtcha owned process doing -bs
Date: Wed, 2 Oct 2002 10:18:01 -0400 (EDT)
From: Igor Pechtchanski <pechtcha AT cs DOT nyu DOT edu>
Reply-To: cygwin AT cygwin DOT com
To: Nrusinh Ambekar <nrusinh AT hotmail DOT com>
cc: cygwin AT cygwin DOT com
Subject: Re: problem - if statement in shell script gives opposite results
with new bash
In-Reply-To: <F139NIJWNDIT6bB5Rw100000304@hotmail.com>
Message-ID: <Pine.GSO.4.44.0210020951470.2690-100000@slinky.cs.nyu.edu>
Importance: Normal
MIME-Version: 1.0

Nrusinh,

On Wed, 2 Oct 2002, Nrusinh Ambekar wrote:

> Hi,
> I have recently downloaded new cygwin. I am getting incorrect results when I
> run simple shell script using bash. The same shell script runs fine with old
> cygwin bash.
>
> Your help is really appreciated.
> thanks,
> Nrusinh

A quick correction: it's not the if statement that gives the opposite
results, it's the "test" command (the "[...]" construct) that returns an
"opposite" value.  This having been said, there are a couple of problems
with your script (see below):

> The script is as follows
>
> #!/usr/local/bin/bash
    ^
Why is your bash in /usr/local/bin?  It's normally installed in /bin (or
/usr/bin, which is usually the same under Cygwin).

> myfunc()
> {
> local item=$1
> shift 1
> local list=" $* "
> echo "item is"
> echo $item
> echo "list is"
> echo $list
> if [ "${item}" = "${list}" ] || [ "${list#"* ${item} *"}" != "${list}" ] || [ "${list#"${item} "}" != "${list}" ] || [ "${list%" ${item}"}" != "${list}" ]
  ^
Your quoting seems off here.  Quotes do not nest in bash (or any other
shells)...  Besides, if you're using the curlies ("{}") to delimit
variable substitutions, you don't need to quote stuff inside the curlies.

Also, your prefix/suffix matches don't seem well-constructed.  The first
one, frankly, doesn't make sense at all - bash cannot match regular
expressions open at both ends.  You're better off with something like
this (which works, btw):

if [ "${item}" = "${list}" ] || [ "${list#* ${item} }" != "${list}" ] || [ "${list% ${item} *}" != "${list}" ]

> then
> echo "found"
> else
> echo "not found"
> fi
> }
>
> myfunc CECA OTA OEA CECA

So, this is not a bash bug after all.  I'm really not sure why the script
used to work...  You probably relied on some bug that was fixed in the
current version.  And, unless you had a link from /usr/local/bin/bash to
/usr/bin/bash (or ran this explicitly with "bash script.bash"), this
wouldn't have worked at all.
	Igor
-- 
				http://cs.nyu.edu/~pechtcha/
      |\      _,,,---,,_		pechtcha AT cs DOT nyu DOT edu
ZZZzz /,`.-'`'    -.  ;-;;,_		igor AT watson DOT ibm DOT com
     |,4-  ) )-,_. ,\ (  `'-'		Igor Pechtchanski
    '---''(_/--'  `-'\_) fL	a.k.a JaguaR-R-R-r-r-r-.-.-.  Meow!

"Water molecules expand as they grow warmer" (C) Popular Science, Oct'02, p.51


--
Unsubscribe info:      http://cygwin.com/ml/#unsubscribe-simple
Bug reporting:         http://cygwin.com/bugs.html
Documentation:         http://cygwin.com/docs.html
FAQ:                   http://cygwin.com/faq/

- Raw text -


  webmaster     delorie software   privacy  
  Copyright © 2019   by DJ Delorie     Updated Jul 2019