Zsh Mailing List Archive
Messages sorted by: Reverse Date, Date, Thread, Author

Re: [[ -f ]] and filename generation

On Apr 16,  1:31pm, Meino.Cramer@xxxxxx 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.

Messages sorted by: Reverse Date, Date, Thread, Author