This is the mail archive of the cygwin mailing list for the Cygwin project.

Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

Re: bash script bugs

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
> 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 ./
> /cygdrive/d/install/buf/*.html is only
> /cygdrive/d/install/buf/a.html

Well of course.

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

is the same as

./ /cygdrive/d/install/buf/a.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
>  cygcheck.out  sources
> and so ./ do

Of course.  Calling ./ 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   
volunteer cygwin bash maintainer
Version: GnuPG v1.4.9 (Cygwin)
Comment: Public key at
Comment: Using GnuPG with Mozilla -


Problem reports:
Unsubscribe info:

Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]