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

Re: [[ -f ]] and filename generation



On 04/16/2016 07:31 AM, Meino.Cramer@xxxxxx 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





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