zsh-users
 help / color / mirror / code / Atom feed
From: Bart Schaefer <schaefer@brasslantern.com>
To: zsh-users@zsh.org
Subject: Re: [[ -f ]] and filename generation
Date: Sat, 16 Apr 2016 10:10:08 -0700	[thread overview]
Message-ID: <160416101008.ZM24340@torch.brasslantern.com> (raw)
In-Reply-To: <20160416113133.GA10973@solfire>

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.


      parent reply	other threads:[~2016-04-16 17:10 UTC|newest]

Thread overview: 3+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2016-04-16 11:31 Meino.Cramer
2016-04-16 14:55 ` Eric Cook
2016-04-16 17:10 ` Bart Schaefer [this message]

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=160416101008.ZM24340@torch.brasslantern.com \
    --to=schaefer@brasslantern.com \
    --cc=zsh-users@zsh.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
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).