delorie.com/archives/browse.cgi   search  
Mail Archives: cygwin/2009/12/02/20:55:25

X-Recipient: archive-cygwin AT delorie DOT com
X-SWARE-Spam-Status: No, hits=-2.0 required=5.0 tests=AWL,BAYES_00,SPF_SOFTFAIL
X-Spam-Check-By: sourceware.org
Message-ID: <4B171A8D.4050706@byu.net>
Date: Wed, 02 Dec 2009 18:55:25 -0700
From: Eric Blake <ebb9 AT byu DOT net>
User-Agent: Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.8.1.23) Gecko/20090812 Thunderbird/2.0.0.23 Mnenhy/0.7.6.666
MIME-Version: 1.0
To: cygwin AT cygwin DOT com, icegood1980 AT gmail DOT com
Subject: Re: bash script bugs
References: <846d84d90912021712j574b7749o9d9548203d21b901 AT mail DOT gmail DOT com>
In-Reply-To: <846d84d90912021712j574b7749o9d9548203d21b901@mail.gmail.com>
X-IsSubscribed: yes
Mailing-List: contact cygwin-help AT cygwin DOT com; run by ezmlm
List-Id: <cygwin.cygwin.com>
List-Subscribe: <mailto:cygwin-subscribe AT cygwin DOT com>
List-Archive: <http://sourceware.org/ml/cygwin/>
List-Post: <mailto:cygwin AT cygwin DOT com>
List-Help: <mailto:cygwin-help AT cygwin DOT com>, <http://sourceware.org/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

-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

According to Sergey Ivanov on 12/2/2009 6:12 PM:
> Have a script:
> #!/usr/bin/bash
> f="$(ls $1)"

The quotes around $(), while nice for consistency, are not strictly
necessary (since $() forms a word without quoting, and variable assignment
is not subject to word splitting).  Meanwhile, you are missing quotes
around $1.  And did you really mean $1, or did you want $@?

> for v_file in "$f"; do

Why are you quoting $f?  That means that v_file will be exactly equal to
one item during the loop: $f, regardless of whether an unquoted $f would
have split into multiple words.

>   echo $v_file

But now, by not quoting $v_file, you run afoul of any IFS issues, such as
a file name containing multiple spaces.

> done
> named FileGroupContentsChange.sh.
> 
> Twos things do not work as they should. By order:
> 1) Output in command line for nest commands
>   ls /cygdrive/d/install/buf/*.html
>   dir /cygdrive/d/install/buf/*.html
> is same:
> /cygdrive/d/install/buf/a.html
> /cygdrive/d/install/buf/b.html
> 
> while output for ./FileGroupContentsChange.sh
> /cygdrive/d/install/buf/*.html is only
> /cygdrive/d/install/buf/a.html

Well of course.

./FileGroupContentsChange.sh /cygdrive/d/install/buf/*.html

is the same as

./FileGroupContentsChange.sh /cygdrive/d/install/buf/a.html
/cygdrive/d/install/buf/b.html

since the shell expands * PRIOR to calling the script.  Thus, when the
script starts, $1 is /cygdrive/d/install/buf/a.html and $2 is
/cygdrive/d/install/buf/b.html, but your script didn't use $2.

And none of this is cygwin-specific.

> 2) (not critical but not "classical") Both command line script and
> outputs for current folder in one line:
> ls returns
> FileGroupContentsChange.sh  cygcheck.out  sources
> and so ./FileGroupContentsChange.sh do

Of course.  Calling ./FileGroupContentsChange.sh without arguments unsets
$1; then since you used $1 without quotes, you ended up calling ls without
arguments, and ls without arguments lists the contents of the current
directory.  Again, none of this is cygwin-specific, nor does it represent
a bug in anything except your expectations of your script.

- --
Don't work too hard, make some time for fun as well!

Eric Blake             ebb9 AT byu DOT net
volunteer cygwin bash maintainer
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.9 (Cygwin)
Comment: Public key at home.comcast.net/~ericblake/eblake.gpg
Comment: Using GnuPG with Mozilla - http://enigmail.mozdev.org/

iEYEARECAAYFAksXGo0ACgkQ84KuGfSFAYDGpgCffC4D/tQbYJsFSjoUkae++HT/
A/MAnRtDC/pYmvcYckDG5KOhK5xeJa6I
=QQA5
-----END PGP SIGNATURE-----

--
Problem reports:       http://cygwin.com/problems.html
FAQ:                   http://cygwin.com/faq/
Documentation:         http://cygwin.com/docs.html
Unsubscribe info:      http://cygwin.com/ml/#unsubscribe-simple

- Raw text -


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