Zsh Mailing List Archive
Messages sorted by:
Reverse Date,
Date,
Thread,
Author
Re: [[ -f ]] and filename generation
- X-seq: zsh-users 21452
- From: Bart Schaefer <schaefer@xxxxxxxxxxxxxxxx>
- To: zsh-users@xxxxxxx
- Subject: Re: [[ -f ]] and filename generation
- Date: Sat, 16 Apr 2016 10:10:08 -0700
- Dkim-signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=brasslantern-com.20150623.gappssmtp.com; s=20150623; h=from:message-id:date:in-reply-to:comments:references:to:subject :mime-version; bh=Fc6MwGdOtK1Iz3Nnf5ppNFFb9m6Q4jI1VFH/4xTsQMc=; b=BY1dGOc7+GnSE/Rs54JxXqRllBQPwy+vL7muUcHE4TebahxMoO7q4aRS7NSIqsGXJu 7ZKd6KglfAEsii2jzYZD/TGJ8+9X1OFn/H0ZOykU9ndn/qpQQZyMCm8i6STG3FRzpq0O tnuliYPrCD4ME6zS0uA14mEMx15UPLPA+KCt1kTZNHnPsCNfUOLn1UuntnOgHVcD8Djw sa1rA4yOjZuBugulJZF2nyEPitKBEAZG2o2vyqXYcfPochJQOvDtDQ01R8I8f+oXwoLB 3+EQJ46Wm0C/U0B7E9oUJcYtfUiW0c+VRZkmx2bJB0Jp7+pMYAelU4zM87bzsCfRVHVo p4sg==
- In-reply-to: <20160416113133.GA10973@solfire>
- List-help: <mailto:zsh-users-help@zsh.org>
- List-id: Zsh Users List <zsh-users.zsh.org>
- List-post: <mailto:zsh-users@zsh.org>
- Mailing-list: contact zsh-users-help@xxxxxxx; run by ezmlm
- References: <20160416113133.GA10973@solfire>
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