zsh-users
 help / color / mirror / code / Atom feed
* [[ -f ]] and filename generation
@ 2016-04-16 11:31 Meino.Cramer
  2016-04-16 14:55 ` Eric Cook
  2016-04-16 17:10 ` Bart Schaefer
  0 siblings, 2 replies; 3+ messages in thread
From: Meino.Cramer @ 2016-04-16 11:31 UTC (permalink / raw)
  To: zsh-users

Hi,

A script I am writing is reading filenames from a list.
These filenames not complete.

Example:

On disk:
filename.ext1.ext2

In the list:
filename
or
filename.ext

I want to check for the existing of the files in the list on disk with
a construct like this

if [[ -f $FILEFROMLIST ]] ; then

    # do something more useful here

fi

I know, that are different approaches to this problem (for example
reading the filenames from disk into an array and process those) but
I am interested in getting this -f thingy to run.

I tried variation of
if [[ -f "$FILEFROMLIST*" ]] ; then .... fi
with extendedglob set...but with no success.

Is there any way to get this "-f" thingy to work?

Thank you very much in advance for any help!
Best regards,
Meino






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

* Re: [[ -f ]] and filename generation
  2016-04-16 11:31 [[ -f ]] and filename generation Meino.Cramer
@ 2016-04-16 14:55 ` Eric Cook
  2016-04-16 17:10 ` Bart Schaefer
  1 sibling, 0 replies; 3+ messages in thread
From: Eric Cook @ 2016-04-16 14:55 UTC (permalink / raw)
  To: zsh-users

On 04/16/2016 07:31 AM, Meino.Cramer@gmx.de wrote:
> Hi,
> 
> A script I am writing is reading filenames from a list.
> These filenames not complete.
> 
> Example:
> 
> On disk:
> filename.ext1.ext2
> 
> In the list:
> filename
> or
> filename.ext
> 
> I want to check for the existing of the files in the list on disk with
> a construct like this
> 
> if [[ -f $FILEFROMLIST ]] ; then
> 
>     # do something more useful here
> 
> fi
> 
> I know, that are different approaches to this problem (for example
> reading the filenames from disk into an array and process those) but
> I am interested in getting this -f thingy to run.
> 
> I tried variation of
> if [[ -f "$FILEFROMLIST*" ]] ; then .... fi
> with extendedglob set...but with no success.
> 
> Is there any way to get this "-f" thingy to work?
> 
> Thank you very much in advance for any help!
> Best regards,
> Meino
> 
> 
> 
> 
> 
Filename generation does not happen inside the [[ normally.
In zsh >= 5.0.5, you can force globbing to happen by using the glob qualifier #q.
(requires extendedglob to be enabled)

% [[ -f *(#q.) ]]; echo $?
1

Since globbing can match multiple files, [[ -f will fail since more than one file
matches the glob. So you can also use the new qualifier Y to limit the results to
the first match.

% [[ -f *(#qY1.) ]]; echo $?
0




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

* Re: [[ -f ]] and filename generation
  2016-04-16 11:31 [[ -f ]] and filename generation Meino.Cramer
  2016-04-16 14:55 ` Eric Cook
@ 2016-04-16 17:10 ` Bart Schaefer
  1 sibling, 0 replies; 3+ messages in thread
From: Bart Schaefer @ 2016-04-16 17:10 UTC (permalink / raw)
  To: zsh-users

On Apr 16,  1:31pm, Meino.Cramer@gmx.de wrote:
}
} A script I am writing is reading filenames from a list.
} These filenames not complete.
} 
} I want to check for the existing of the files in the list on disk with
} a construct like this
} 
} if [[ -f $FILEFROMLIST ]] ; then

Assuming this test returns true, do you thereafter need the names of the
actual files, or do you only care that some such files exist?  If you
will need the filenames anyway, globbing them into an array and then
testing that the array is not empty is probably most efficient.

If you only care that at least one such name exists, then Eric's (#qY1)
solution is on the right track, but you probably want

    if [[ -f "$FILEFROMLIST"*(#q.NY1) ]] ; then ...

to check only plain files (not directories) and to avoid "no match"
errors (unless you commonly have NO_NOMATCH set).  Note the wildcard
is outside the double-quotes.

If you want to test more complicated conditions, such as that ALL the
matching names on disk are plain files, then it gets more difficult
to avoid globbing them into an array and processing each name.

One silly example:

    if test -d . "$FILEFROMLIST"*(P:-a:P:-f:N) ; then ...

The (P) glob flag prepends the argument strings to each glob result
as a separate word, so this produces something like

  test -d . -a -f filename -a -f filename.ext -a -f filename.ext1.ext2

which will fail if any of the matching names is not a plain file.  But
note this will NOT work with the [[ ]] syntax; globbing there cannot
introduce new conditional operators.


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

end of thread, other threads:[~2016-04-16 17:10 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2016-04-16 11:31 [[ -f ]] and filename generation Meino.Cramer
2016-04-16 14:55 ` Eric Cook
2016-04-16 17:10 ` Bart Schaefer

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).