Glob in a variable

Glob in a variable

Post by Robert Gea » Fri, 10 Oct 2003 00:16:27



Folks,

I want to put a glob in a variable. Seems feasible, what am i
missing?

Here is what I have:

#!/usr/bin/ksh
Dir=/tmp
Try="*.$$"

for n in 1 2 3 4 5 6 7 8
do
  touch $Dir/file.$n.$$
done

# [ -f $Dir/*.$$ ] && rm $Dir/*.$$ ## What I want to do in a var. used
to rm some tmp files

 [ -f "$Dir/$Try" ] && rm "$Dir/$Try" what im attempting to do

 echo $Dir/$Try  Gives me the correct list ...
 ie. /tmp/file.1.1001 /tmp/file.2./1001 ...

 and echo "$Dir/$Try"  gives  /tmp/*.$$

 
 
 

Glob in a variable

Post by Barry Margoli » Fri, 10 Oct 2003 00:40:05




>Folks,

>I want to put a glob in a variable. Seems feasible, what am i
>missing?

>Here is what I have:

>#!/usr/bin/ksh
>Dir=/tmp
>Try="*.$$"
...
> echo $Dir/$Try  Gives me the correct list ...
> ie. /tmp/file.1.1001 /tmp/file.2./1001 ...

> and echo "$Dir/$Try"  gives  /tmp/*.$$

As you've discovered, quoting prevents wildcard expansion.  So leave out
the quotes.

--

Level(3), Woburn, MA
*** DON'T SEND TECHNICAL QUESTIONS DIRECTLY TO ME, post them to newsgroups.
Please DON'T copy followups to me -- I'll assume it wasn't posted to the group.

 
 
 

Glob in a variable

Post by Robert Gea » Fri, 10 Oct 2003 07:11:19





> >Folks,

> >I want to put a glob in a variable. Seems feasible, what am i
> >missing?

> >Here is what I have:

> >#!/usr/bin/ksh
> >Dir=/tmp
> >Try="*.$$"
>  ...
> > echo $Dir/$Try  Gives me the correct list ...
> > ie. /tmp/file.1.1001 /tmp/file.2./1001 ...

> > and echo "$Dir/$Try"  gives  /tmp/*.$$

> As you've discovered, quoting prevents wildcard expansion.  So leave out
> the quotes.

Yes... nothing like the obvious to bite you in the a**
 
 
 

Glob in a variable

Post by Chris F.A. Johnso » Fri, 10 Oct 2003 07:36:25






>> >Folks,

>> >I want to put a glob in a variable. Seems feasible, what am i
>> >missing?

>> >Here is what I have:

>> >#!/usr/bin/ksh
>> >Dir=/tmp
>> >Try="*.$$"
>>  ...
>> > echo $Dir/$Try  Gives me the correct list ...
>> > ie. /tmp/file.1.1001 /tmp/file.2./1001 ...

>> > and echo "$Dir/$Try"  gives  /tmp/*.$$

>> As you've discovered, quoting prevents wildcard expansion.  So leave out
>> the quotes.

     As an aside, note that the glob is not expanded when it is
     assigned to the variable even without the quotes:

$ a=file*
$ echo "$a"
file*
$ echo $a
file1 file2 file3 file4 file5 file6 file7 file8 file9

Quote:> Yes... nothing like the obvious to bite you in the a**

     Shouldn't that be "in the a??"?

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

 
 
 

Glob in a variable

Post by Stephane CHAZELA » Fri, 10 Oct 2003 07:59:32


[...]

Quote:>      As an aside, note that the glob is not expanded when it is
>      assigned to the variable even without the quotes:

> $ a=file*
> $ echo "$a"
> file*
> $ echo $a
> file1 file2 file3 file4 file5 file6 file7 file8 file9

Yes, but, with ksh, you can expand the pattern into an array.

set -A list -- file*

Note that that doesn't work correctly if there's no matching
file (you "file*" as the only element instead of no element).



  something with "$file"
done

--
Stphane

 
 
 

Glob in a variable

Post by laura_fairhe » Fri, 10 Oct 2003 09:36:51




>>> As you've discovered, quoting prevents wildcard expansion.  So leave out
>>> the quotes.

>     As an aside, note that the glob is not expanded when it is
>     assigned to the variable even without the quotes:

>$ a=file*
>$ echo "$a"
>file*
>$ echo $a
>file1 file2 file3 file4 file5 file6 file7 file8 file9

One thing to watch out for is system differences w.r.t quote characters
within the variable when doing filename expansion from a variable, eg;

$ touch 'file\\'
$ a='file\*'
$ set -x
$ : $a

On some systems the last line will match the file 'file\\' because
the variable $a is interpreted as a filename pattern for example in
pdksh and NetBSD/sh you get;

+ : file\\

On other systems the backslash actually protects the file pattern '*'
so for example in bash you get;

+ : 'file\*'

Another problem with putting entire patterns in variables is with
ksh brace expansion (also done after the variable substitution),
e.g (pdksh);

$ a='a{1,2}*'
$ touch 'a{1,2}'
$ set -x
$ : $a
+ : a1* a2*
$

Oh, and I think the OP meant 'ankle' Chris - then his pattern would
match perfectly :)

seeyafrom
l

 
 
 

1. ISO: csh glob to ksh93 glob idea

I ran across a note the other day which referenced:
/usr/src/perl/{lib,pod}/{,*}/*.p{od,m}

How in ksh93 does one get the equivalent of this csh glob?

I thought perhaps

/usr/src/perl/?(lib|pod)/?(|*)/*.p?(od|m)

but some playing with that didn't seem to work.
--

<*> O- <URL:http://www.teraform.com/%7Elvirden/> only planning.
Unless explicitly stated to the contrary, nothing in this posting
should be construed as representing my employer's opinions.

2. where can i find aix installation help ?

3. Duelling shells (Re: shell architecture (to glob or not to glob))

4. What's the impact to system if I change tcp parameter "tcp_keepalive_interval "

5. Setting variables in a variable array of null variables

6. Loop device

7. Q: Using glob()?

8. ``No more processes...''

9. Patching shared library symbols from GLOB to LOCL

10. How to do command/glob substitution in `setenv'?

11. glob() problem in libc.5.3.12

12. Is there a big performance penalty for using glob()?

13. ftpd build fails on glob.c