zsh-users
 help / color / mirror / code / Atom feed
* Use of variables in the <x-y> globbing mechanism
@ 2016-04-21  5:24 Henman
  2016-04-21  7:15 ` Bart Schaefer
  0 siblings, 1 reply; 3+ messages in thread
From: Henman @ 2016-04-21  5:24 UTC (permalink / raw)
  To: zsh-users


It would be very useful if  the <x-y> globbing mechanism would work with 
variables.   I can't get it to work.  If it only accepts numeric 
constants, nothing can be done, but it would be an elegant solution to 
my problem of taking a slice of files out of a larger group for 
processing in a batch file.


I am trying to do this:   ( Where the Src FName Patter  SFNPAT gives the 
fname prefix and fname extension and the <01-15> specifies which slice 
of files to get.
# The from_number and to_number are read in from a list of files to process
     and injected between a filename pattern's prefix and extension.

    for file in ${SFNPAT:r}-<${from_number}-${to_number}>.${SFNPAT:e}
       do
           # processing goes here
       done

When I try the above script, I am greeted with a  " parse error near 
`<'"    error message.  This can be done otherways, I just wanted to 
know if the <X-Y> form could be used in some way to acheive this.

Thanks


^ permalink raw reply	[flat|nested] 3+ messages in thread

* Re: Use of variables in the <x-y> globbing mechanism
  2016-04-21  5:24 Use of variables in the <x-y> globbing mechanism Henman
@ 2016-04-21  7:15 ` Bart Schaefer
  2016-04-22 11:05   ` Nikolay Aleksandrovich Pavlov (ZyX)
  0 siblings, 1 reply; 3+ messages in thread
From: Bart Schaefer @ 2016-04-21  7:15 UTC (permalink / raw)
  To: zsh-users

On Apr 20, 10:24pm, Henman wrote:
} 
} It would be very useful if  the <x-y> globbing mechanism would work with 
} variables.

It can't do so because <$x-$y> is valid redirection syntax, meaning to
read from file "$x-$y" and write to whatever comes after.  It's rare
enough to have files with digit-digit names that it was deemed OK to
break the redirection rules in that special case, but it would cause
to many problems to generalize it.

} # The from_number and to_number are read in from a list of files to process
}      and injected between a filename pattern's prefix and extension.
} 
}     for file in ${SFNPAT:r}-<${from_number}-${to_number}>.${SFNPAT:e}

Use the {x..y} syntax:

    for file in ${SFNPAT:r}-{${from_number}..${to_number}}.${SFNPAT:e}

This latter expands to the full list of names rather than globbing them,
so it won't skip any missing files, but it will probably work in your
example.

If there may be gaps in the file sequence so you really do need to do
the glob, put the entire expression in a variable like so:

    local slice="<${from_number}-${to_number}>"
    for file in ${SFNPAT:r}-${~slice}.${SFNPAT:e}


^ permalink raw reply	[flat|nested] 3+ messages in thread

* Re: Use of variables in the <x-y> globbing mechanism
  2016-04-21  7:15 ` Bart Schaefer
@ 2016-04-22 11:05   ` Nikolay Aleksandrovich Pavlov (ZyX)
  0 siblings, 0 replies; 3+ messages in thread
From: Nikolay Aleksandrovich Pavlov (ZyX) @ 2016-04-22 11:05 UTC (permalink / raw)
  To: Bart Schaefer, zsh-users

21.04.2016, 10:18, "Bart Schaefer" <schaefer@brasslantern.com>:
> On Apr 20, 10:24pm, Henman wrote:
> }
> } It would be very useful if the <x-y> globbing mechanism would work with
> } variables.
>
> It can't do so because <$x-$y> is valid redirection syntax, meaning to
> read from file "$x-$y" and write to whatever comes after. It's rare
> enough to have files with digit-digit names that it was deemed OK to
> break the redirection rules in that special case, but it would cause
> to many problems to generalize it.
>
> } # The from_number and to_number are read in from a list of files to process
> } and injected between a filename pattern's prefix and extension.
> }
> } for file in ${SFNPAT:r}-<${from_number}-${to_number}>.${SFNPAT:e}
>
> Use the {x..y} syntax:
>
>     for file in ${SFNPAT:r}-{${from_number}..${to_number}}.${SFNPAT:e}
>
> This latter expands to the full list of names rather than globbing them,
> so it won't skip any missing files, but it will probably work in your
> example.
>
> If there may be gaps in the file sequence so you really do need to do
> the glob, put the entire expression in a variable like so:
>
>     local slice="<${from_number}-${to_number}>"
>     for file in ${SFNPAT:r}-${~slice}.${SFNPAT:e}

Variable can be avoided with the usual ${:-…} hack:

    for file in ${SFNPAT:r}-${~${:-<$from_number-$to_number>}}.${SFNPAT:e}


^ permalink raw reply	[flat|nested] 3+ messages in thread

end of thread, other threads:[~2016-04-22 11:11 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2016-04-21  5:24 Use of variables in the <x-y> globbing mechanism Henman
2016-04-21  7:15 ` Bart Schaefer
2016-04-22 11:05   ` Nikolay Aleksandrovich Pavlov (ZyX)

Code repositories for project(s) associated with this public inbox

	https://git.vuxu.org/mirror/zsh/

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).