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

Re: counting trouble



On Wed, Apr 4, 2018 at 11:21 AM, Ray Andrews <rayandrews@xxxxxxxxxxx> wrote:
>
>     tmp=( $1*(N) )
>
>     if [[ "$#tmp" > 1 ]]; then
>
> Alas, if there is only one match then the count becomes a count not of lines
> but of characters which is a nasty gotcha.

That can't be all there is to it.  $#array only becomes a count of
characters if the context forces the array into string form.  Which
admittedly can happen in some non-obvious ways, but what you've shown
above is not one of those ways.  What are you really doing?

> Hacking away I find this works:
>
>     [ -e "$1" ] && tmp=( "${(f)${1}}" ) || tmp=( ${1}*(N) )

Umm ...  [ -e "$1" ] will only succeed for a single literal file name,
not a pattern, and the (f) flag means to split on newlines, so unless
you have a file with newlines embedded in the name (in which case this
is entirely broken a different way) there's no reason for ${(f)...}
there at all.  And none of this has anything to do with how [[ $#tmp >
1 ]] would work after the assignment.

> expected this to work: " ${(f)${1}*} " but it doesn't.

Expected it to do what?  Filename generation like ${1}* out in the
open (so to speak)?  What led you to expect that?  In any case
filename generation would never produce a newline-separated string
(again barring files with newlines in the name), so (f) would not do
anything to the result.

Please don't think about globbing as returning "lines".  It doesn't.



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