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

Re: more splitting



I'm
hoping this hex viewer will let me see all splitting, be it by spaces or
newlines or nuls. Next time I have a splitting headache, I'll be able to
view the data in question with absolute clarity.

Bart gave you a version of hex that shows you exactly what's inside a variable. Here is a version of hex that shows you exactly what's at the front door and what's at the back door. It can come in handy if you ever wonder whether what you are passing to a function is indeed what you think it is.

function hex()
{
  if (($#)); then
    echo "--- Front door --------------"
    print -rC1 -- ${(q+)@}
    echo "\n-----------------------------\n"
    for element ("$@") print -rn -- $element | od -vAx -tx1 -tc
  fi
  if [[ -p /dev/fd/0 ]]; then
    local var c; while read -u 0 -k 1 c; do var+=$c; done
    echo "--- Back door ---------------"
    print -rC1 -- ${(q+)var}
    echo "\n-----------------------------\n"
    for element ("$var") print -rn -- $element | od -vAx -tx1 -tc
  fi
}


The line "local var c; while read -u 0 -k 1 c; do var+=$c; done" is rather verbose and most likely very inefficient but it's the only way I know to fully read what's at the back door including any trailing newline characters.

Usage example:

% print -rn xx " y y " $'z\nz\n\n' | hex aa " b b " $'c\nc\n\n'
--- Front door --------------
aa
' b b '
$'c\nc\n\n'

-----------------------------

0000000    61  61                                                        
           a   a                                                        
0000002
0000000    20  62  20  62  20                                            
               b       b                                                
0000005
0000000    63  0a  63  0a  0a                                            
           c  \n   c  \n  \n                                            
0000005
--- Back door ---------------
$'xx  y y  z\nz\n\n'

-----------------------------

0000000    78  78  20  20  79  20  79  20  20  7a  0a  7a  0a  0a        
           x   x           y       y           z  \n   z  \n  \n        
000000e

Philippe
 


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