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

triviality regarding $# counts



This practice text is in a variable 'temp' and I want to count the number of lines inside a function that will also print the text exactly as shown, and do a bunch of other stuff.

--------------------------------------------------------------------
xz-utils:
/usr/bin/xzgrep
/usr/share/man  /de/man1/xzgrep.1.gz    # Hafta be careful not to split here.
/usr/share/man  /ko/man1/xzgrep.1.gz    # ... or here.
/usr/share/man/man1/xzgrep.1.gz
/usr/share/man/pt_BR/man1/xzgrep.1.gz
/usr/share/man/ro/man1/xzgrep.1.gz
/usr/share/man/uk/man1/xzgrep.1.gz
/usr/bin/xzegrep
/usr/bin/xzfgrep
/usr/share/man/de/man1/lzegrep.1.gz
/usr/share/man/de/man1/lzfgrep.1.gz
/usr/share/man/uk/man1/xzegrep.1.gz
/usr/share/man/uk/man1/xzfgrep.1.gz

zsh-common:
/usr/share/doc/zsh-common/examples/Functions/zpgrep
/usr/share/zsh/functions/Completion/Debian/_grep-excuses
/usr/share/zsh/functions/Completion/Unix/_grep
/usr/share/zsh/functions/Completion/Unix/_ngrep
/usr/share/zsh/functions/Completion/Unix/_pgrep

zstd:
/usr/bin/zstdgrep
/usr/share/man/man1/zstdgrep.1.gz
-----------------------------------------------------------------------------------

This works: (Again, this is deep in a function and I have to use eval.)

    output=$( eval "$@" )   # '$@' will expand to 'print -l $temp' which is the text above.
    temp=( $( eval $@ ) )    # To get the correct count I need to force an array.
    linecount=$#temp
    print -rl -- $output
    print  $linecount

Various experiments trying to get the correct linecount (23) *and* get it printing correctly all end up with waterboarding problems.  If I do it in two stages as above, it's ok, but is seems very clumsy.  Different efforts at quoting or using ' ${(@f) ....} ' and various other tricks yield me a linecount of 1, 3, 23, 25, 26, or 738.  And output that deletes the blank lines, or forces everything into one 'line/element'.  Basically I need the array form to to get the line count, but it won't print properly as an array.  Not that it's worth much trouble, but is it possible to get the variable  to print correctly *and* show the count of lines without having to eval it twice?  schematically:

    output=????$( eval ???)??? # Whatever the correct code might be.
    linecount=$#output
    print -rl -- $output
    print  $linecount

... as it is, it seems that '$#' never counts the lines of output as it actually prints.  In practice it's hardly a problem but still it bothers me.  '$#' seems mostly to want to count characters (738). 





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