Forum: Ruby Ruby wildcard command line argument auto expansion

39d431ce99f9f3e7018d38489e1d573d?d=identicon&s=25 Michael Jia (mjia)
on 2007-06-09 11:39
I want to pass in wildcard file names, and use it to match file names
located in different directories. For example, *.txt will  match file
such as "my.txt", "you.txt", etc.

However, I found out ruby intepreter automatically expands "*.txt"
command argument to a array of filenames which matches that wildcard in
the current directory.

For example:

C:\working>dir *.txt

05/10/2007 03:24 PM 46,101      config.txt
11/23/2004 11:54 AM 361           tips.txt
2 File(s) 46,462 bytes

If you do,

C:\working>ruby -e "puts ARGV" *.txt
config.txt
tips.txt

Ruby converts string *.txt into the matching filenames and pass in the
expanded array as the new argument.

This *nice* trick sometime creates trouble.

In my case, I want to use 'ARGV[0]' to match filenames in a different
location. But ARGV[0] is not "*.txt" as my expected. It was changed by
ruby. In fact, it is "config.txt" in this case.

One way to correct it is to always ask user to use single-quoted string:

C:\working>ruby -e "puts ARGV" '*.txt'
*.txt
39d431ce99f9f3e7018d38489e1d573d?d=identicon&s=25 Michael Jia (mjia)
on 2007-06-09 11:41
Is there a better way to disbale this auto expansion?

Thanks
Michael

> One way to correct it is to always ask user to use single-quoted string:
>
> C:\working>ruby -e "puts ARGV" '*.txt'
> *.txt
3afd3e5e05dc9310c89aa5762cc8dd1d?d=identicon&s=25 Tim Hunter (Guest)
on 2007-06-09 13:14
(Received via mailing list)
Michael Jia wrote:
>>
>
>
>
The filename expansion is a function of the command window, not Ruby. By
the time Ruby starts up the expansion has already happened. I think the
only way to disable it is to put quotes around the argument.
6e52043547bee09dd94a90cceadb140f?d=identicon&s=25 Gregor Schmidt (Guest)
on 2007-06-09 13:40
(Received via mailing list)
Hallo Michael,

it's not Ruby, that expands your argument, but the shell. And
therefore you got no other chance than escaping the argument there.

This can be done using quotes or a backslash in front of each special
character
> ruby -e "puts ARGV" \*.txt

Best,

Gregor
39d431ce99f9f3e7018d38489e1d573d?d=identicon&s=25 Michael Jia (mjia)
on 2007-06-09 19:39
Thanks you all for the reply.
I blog this thread in
http://zhifengjia.spaces.live.com/blog/cns!5D88F9E...
Hopefully, it will appear in search engine's list to save ppl time.


Gregor Schmidt wrote:
> Hallo Michael,
>
> it's not Ruby, that expands your argument, but the shell. And
> therefore you got no other chance than escaping the argument there.
>
> This can be done using quotes or a backslash in front of each special
> character
>> ruby -e "puts ARGV" \*.txt
>
> Best,
>
> Gregor
39d431ce99f9f3e7018d38489e1d573d?d=identicon&s=25 Michael Jia (mjia)
on 2007-06-09 20:18
I did some further research on this topic. My win xp cmd shell seems
doesn't do wildcard expansion by itself.

C:\working>echo *.txt
*.txt

There are some posts saying the VC C runtime library is the one actually
does wildcard expansion. As seen here:
http://www.lyra.org/pipermail/scite-interest/2002-...



A similar behavior in Java was also reported here,
http://bugs.sun.com/bugdatabase/view_bug.do?bug_id=6328875
http://mindprod.com/jgloss/main.html
http://groups.google.com/group/comp.lang.java.prog...

It seems to me that it is the ruby intepreter was linked to the runtime
library, namely, startargv.obj, then it gets the expansion for free. :)

Can someone familiar with ruby build please clarify?
Ef3aa7f7e577ea8cd620462724ddf73b?d=identicon&s=25 Rob Biedenharn (Guest)
on 2007-06-09 21:16
(Received via mailing list)
On Jun 9, 2007, at 2:18 PM, Michael Jia wrote:

>
> It seems to me that it is the ruby intepreter was linked to the
> runtime
> library, namely, startargv.obj, then it gets the expansion for
> free. :)
>
> Can someone familiar with ruby build please clarify?
>
> --
> Posted via http://www.ruby-forum.com/.

rab:Users/rab $ mkdir tmp
rab:Users/rab $ cd tmp
rab:rab/tmp $ touch foo.txt bar.pdf baz.rb
rab:rab/tmp $ echo *.txt
foo.txt
rab:rab/tmp $ echo *.pdf
bar.pdf
rab:rab/tmp $ echo *.java
*.java
rab:rab/tmp $

It also depends what is in the directory at the time of the
expansion.  If there are no files that match the glob pattern, the
shell (bash in this case) passes the argument as is (just as if it
had been '*.java').

rab:rab/tmp $ ruby -e 'puts ARGV.join(" - ")' *.*
bar.pdf - baz.rb - foo.txt
rab:rab/tmp $ ruby -e 'puts ARGV.join(" - ")' *.txt
foo.txt
rab:rab/tmp $ ruby -e 'puts ARGV.join(" - ")' *.pdf
bar.pdf
rab:rab/tmp $ ruby -e 'puts ARGV.join(" - ")' *.java
*.java
rab:rab/tmp $ ruby -e 'puts ARGV.join(" - ")' b*
bar.pdf - baz.rb
rab:rab/tmp $ ruby -e 'puts ARGV.join(" - ")' *f*
bar.pdf - foo.txt
rab:rab/tmp $ ruby -e 'puts ARGV.join(" - ")' '*f*'
*f*

-Rob

Rob Biedenharn    http://agileconsultingllc.com
Rob@AgileConsultingLLC.com
39d431ce99f9f3e7018d38489e1d573d?d=identicon&s=25 Michael Jia (mjia)
on 2007-06-09 23:30
I am using Windows XP "cmd" shell. What you described is true for unix
bash or C-shell.


C:\working>dir *.txt
 Volume in drive C has no label.
 Volume Serial Number is 5471-FCF7

 Directory of C:\working

05/10/2007  03:24 PM            46,101 config.txt
11/23/2004  11:54 AM               361 tips.txt
               2 File(s)         46,462 bytes
               0 Dir(s)  32,580,259,840 bytes free

C:\working>echo *.txt
*.txt


That is why I think it is the ruby intepreter in Windows does the
expansion.

Regards
Michael


Rob Biedenharn wrote:
> On Jun 9, 2007, at 2:18 PM, Michael Jia wrote:
>
>>
>> It seems to me that it is the ruby intepreter was linked to the
>> runtime
>> library, namely, startargv.obj, then it gets the expansion for
>> free. :)
>>
>> Can someone familiar with ruby build please clarify?
>>
>> --
>> Posted via http://www.ruby-forum.com/.
>
> rab:Users/rab $ mkdir tmp
> rab:Users/rab $ cd tmp
> rab:rab/tmp $ touch foo.txt bar.pdf baz.rb
> rab:rab/tmp $ echo *.txt
> foo.txt
> rab:rab/tmp $ echo *.pdf
> bar.pdf
> rab:rab/tmp $ echo *.java
> *.java
> rab:rab/tmp $
>
> It also depends what is in the directory at the time of the
> expansion.  If there are no files that match the glob pattern, the
> shell (bash in this case) passes the argument as is (just as if it
> had been '*.java').
>
> rab:rab/tmp $ ruby -e 'puts ARGV.join(" - ")' *.*
> bar.pdf - baz.rb - foo.txt
> rab:rab/tmp $ ruby -e 'puts ARGV.join(" - ")' *.txt
> foo.txt
> rab:rab/tmp $ ruby -e 'puts ARGV.join(" - ")' *.pdf
> bar.pdf
> rab:rab/tmp $ ruby -e 'puts ARGV.join(" - ")' *.java
> *.java
> rab:rab/tmp $ ruby -e 'puts ARGV.join(" - ")' b*
> bar.pdf - baz.rb
> rab:rab/tmp $ ruby -e 'puts ARGV.join(" - ")' *f*
> bar.pdf - foo.txt
> rab:rab/tmp $ ruby -e 'puts ARGV.join(" - ")' '*f*'
> *f*
>
> -Rob
>
> Rob Biedenharn    http://agileconsultingllc.com
> Rob@AgileConsultingLLC.com
Ef3aa7f7e577ea8cd620462724ddf73b?d=identicon&s=25 Rob Biedenharn (Guest)
on 2007-06-10 00:10
(Received via mailing list)
In that case, you should run these two commands:

c:\working> ruby -e "puts ARGV.join(' - ')"  *.txt

c:\working> ruby -e "puts ARGV.join(' - ')" "*.txt"

and they should produce the same output
(i.e., "config.txt - tips.txt" or "tips.txt - config.txt")
if the work is being done by the interpreter.

If one of them produces simply "*.txt" (probably the second), then
something different is going on.

-Rob
39d431ce99f9f3e7018d38489e1d573d?d=identicon&s=25 Michael Jia (mjia)
on 2007-06-10 00:16
Rob, the result is:

C:\working>ruby -e "puts ARGV.join(' - ')"  *.txt
config.txt - tips.txt

C:\working>ruby -e "puts ARGV.join(' - ')" "*.txt"
config.txt - tips.txt

Thanks
Michael


Rob Biedenharn wrote:
> In that case, you should run these two commands:
>
> c:\working> ruby -e "puts ARGV.join(' - ')"  *.txt
>
> c:\working> ruby -e "puts ARGV.join(' - ')" "*.txt"
>
> and they should produce the same output
> (i.e., "config.txt - tips.txt" or "tips.txt - config.txt")
> if the work is being done by the interpreter.
>
> If one of them produces simply "*.txt" (probably the second), then
> something different is going on.
>
> -Rob
Ef3aa7f7e577ea8cd620462724ddf73b?d=identicon&s=25 Rob Biedenharn (Guest)
on 2007-06-10 04:55
(Received via mailing list)
OK, then it look like your 'ruby' really is doing some form of
filename expansion for command-line arguments.  However, not using
Windows myself, there's no way that I could help you more than this.
I do find it interesting though.

-Rob

On Jun 9, 2007, at 6:16 PM, Michael Jia wrote:

>
>>
>> If one of them produces simply "*.txt" (probably the second), then
>> something different is going on.
>>
>> -Rob

Rob Biedenharn    http://agileconsultingllc.com
Rob@AgileConsultingLLC.com
A94d89bceafe0a9dfee54f8cb9bdc6f2?d=identicon&s=25 Shin guey Wong (sgwong)
on 2007-06-11 04:17
I think you should use '*.txt' instead of "*.txt". Here the output from
my WinXP:

C:\>ruby -e "puts ARGV.join(' - ')" *.txt
atlog.txt - ClientCfg.txt - dfsinfo.txt - ipconfig.txt -
javainstalls.txt - NewG
zCompressedFile.txt - sdatlog.txt - setregion.txt - trace.txt

C:\>ruby -e "puts ARGV.join(' - ')" '*.txt'
*.txt

C:\>ruby -e "puts ARGV.join(' - ')" "*.txt"
atlog.txt - ClientCfg.txt - dfsinfo.txt - ipconfig.txt -
javainstalls.txt - NewG
zCompressedFile.txt - sdatlog.txt - setregion.txt - trace.txt

C:\>ruby --version
ruby 1.8.6 (2007-03-13 patchlevel 0) [i386-mswin32]

C:\>
F1d6cc2b735bfd82c8773172da2aeab9?d=identicon&s=25 Nobuyoshi Nakada (Guest)
on 2007-06-11 05:26
(Received via mailing list)
Hi,

At Sat, 9 Jun 2007 18:39:30 +0900,
Michael Jia wrote in [ruby-talk:254917]:
> In my case, I want to use 'ARGV[0]' to match filenames in a different
> location. But ARGV[0] is not "*.txt" as my expected. It was changed by
> ruby. In fact, it is "config.txt" in this case.
>
> One way to correct it is to always ask user to use single-quoted string:
>
> C:\working>ruby -e "puts ARGV" '*.txt'
> *.txt

It's the only way.
31bb0c6ac470d40572c054ce8a495fa6?d=identicon&s=25 Paul Denize (pdenize)
on 2009-06-16 02:15
Shin guey Wong wrote:
> I think you should use '*.txt' instead of "*.txt". Here the output from
> my WinXP:

I think Ruby is wrong to change the OS standard.  On unix the standard
is to expand as filenames (fine).  On windows the standard is NOT to
expand wildcards.

I use wildcards for non filename lookups (i.e. to match users in an
LDAP)

Example in Pthon
C:\> finduser.py pa*
user paul found

Same in Ruby
C:\> finduser.rb pa*
- and it goes looking for a user with a name matching some file in the
current directory.

I accept that on unix it SHOULD require quotes or escaping but not on
Windows.

On windows the ARGV should be "pa*" as typed.
Please log in before posting. Registration is free and takes only a minute.
Existing account

NEW: Do you have a Google/GoogleMail, Yahoo or Facebook account? No registration required!
Log in with Google account | Log in with Yahoo account | Log in with Facebook account
No account? Register here.