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

Re: Fwd: more splitting travails




On 2024-01-12 14:09, Bart Schaefer wrote:
Yeah, oddly, there's no straightforward way to get an unaltered file
into a shell variable.  Even
  read -rd '' < file
trims off trailing newlines. 

In my situation that would be welcome, tho on principle I do wish the culture was: 'If I want something done to my data I'll ask for it'.

I'm expecting Roman or someone to point out a different trick I've forgotten.

Roman would know.  I have my utility working quite well,   where the input isn't an array it's very simple:

    % varis variable

Without the dollar sign.  It evals it internally *after* capturing the name. You put the utility inside some function to trace variable values, it prints out like:


function test1 ()
{

local my_variable="Shall I compare thee to a summer\'s day?"
varis my_variable "some comment"
}

0 /aWorking/Zsh/Source/Wk 1 % . test1; test1

...

test1():4 in: test1:6 > "$my_variable" is: |Shall I compare thee to a summer's day?| some comment - 18:33:06

... It reports name of function, logical line, running file, physical line, name of variable, content, some comment if present and the time.  I love it.  Anyway, it was all good except that arrays print on multiple lines if requested and they were not showing any blank lines, which I object to on principle -- so I'm trying to fix that.  It's 99% there:

0 /aWorking/Zsh/Source/Wk 1 % print -l "$vvar[@]"     # What it really looks like.                         
one two
three

four

five six seven


eight

0 /aWorking/Zsh/Source/Wk 1 % varis ,m vvar ! this is comment     # squashed array, usually fine but ...

zsh():15 in: zsh:15 > "$vvar" is:
one two
three
four
five six seven
eight
! this is comment - 18:41:06

0 /aWorking/Zsh/Source/Wk 1 % varis ,m "${(@f)vvar}" ! this is comment     # ... when I'm determined to see the blanks:

zsh():16 in: zsh:16 > RAW:
one two
three

four

five six seven


eight




this is comment - 18:41:13


... as discussed I have to twist the shell's arm to get my file into a variable as it actually is.  In practice it's not a big deal but on principle it would be nice to avoid "  "${(@f) var}"  "

This sure looks good:

0 /aWorking/Zsh/Source/Wk 0 % read -rd '' < testfile2 aaa

0 /aWorking/Zsh/Source/Wk 0 % print -l $aaa     # Sheesh, it's so simple here.       
one two
three

four

five six seven


eight




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