zsh-users
 help / color / mirror / code / Atom feed
* read string containing null
@ 2021-10-21 12:35 Pier Paolo Grassi
  2021-10-21 13:08 ` Peter Stephenson
  0 siblings, 1 reply; 5+ messages in thread
From: Pier Paolo Grassi @ 2021-10-21 12:35 UTC (permalink / raw)
  To: Zsh-Users List

[-- Attachment #1: Type: text/plain, Size: 229 bytes --]

Hello, I just found out:

echo -e - a$'\000'b | IFS='' builtin read -r -d '' xx; echo -n - $xx
result is "a"

is it possible for builtin read not to stop an null bytes? I found nothing
in documentation.
thanks

Pier Paolo Grassi

[-- Attachment #2: Type: text/html, Size: 559 bytes --]

^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: read string containing null
  2021-10-21 12:35 read string containing null Pier Paolo Grassi
@ 2021-10-21 13:08 ` Peter Stephenson
  2021-10-21 13:30   ` Pier Paolo Grassi
  2021-10-22  9:05   ` Stephane Chazelas
  0 siblings, 2 replies; 5+ messages in thread
From: Peter Stephenson @ 2021-10-21 13:08 UTC (permalink / raw)
  To: Pier Paolo Grassi, Zsh-Users List

> On 21 October 2021 at 13:35 Pier Paolo Grassi <pierpaolog@gmail.com> wrote:
> Hello, I just found out:
> 
> echo -e - a$'\000'b | IFS='' builtin read -r -d '' xx; echo -n - $xx
> result is "a"
> 
> is it possible for builtin read not to stop an null bytes? I found nothing
> in documentation.
> thanks

I think -d '' is telling it to treat NULL bytes as a delimiter.  Take that out.

If you're not trying to read a line, don't use read.  If you "zmodload zsh/system"
there's a sysread command which is more useful for lower level operations.

pws


^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: read string containing null
  2021-10-21 13:08 ` Peter Stephenson
@ 2021-10-21 13:30   ` Pier Paolo Grassi
  2021-10-22  9:05   ` Stephane Chazelas
  1 sibling, 0 replies; 5+ messages in thread
From: Pier Paolo Grassi @ 2021-10-21 13:30 UTC (permalink / raw)
  To: Peter Stephenson; +Cc: Zsh-Users List

[-- Attachment #1: Type: text/plain, Size: 1021 bytes --]

Yes, I want to read the whole stdin in a single operation, so the -d was
there to disable stopping at newline
It seem that instead made read stop at null bytes
I worked around this limitation with:

xx=$(echo -e - a$'\000'b; echo .); xx=${xx[1,-2]}; echo -n - $xx
ab

but I will try also your suggestion, thanks

Pier Paolo Grassi


Il giorno gio 21 ott 2021 alle ore 15:08 Peter Stephenson <
p.w.stephenson@ntlworld.com> ha scritto:

> > On 21 October 2021 at 13:35 Pier Paolo Grassi <pierpaolog@gmail.com>
> wrote:
> > Hello, I just found out:
> >
> > echo -e - a$'\000'b | IFS='' builtin read -r -d '' xx; echo -n - $xx
> > result is "a"
> >
> > is it possible for builtin read not to stop an null bytes? I found
> nothing
> > in documentation.
> > thanks
>
> I think -d '' is telling it to treat NULL bytes as a delimiter.  Take that
> out.
>
> If you're not trying to read a line, don't use read.  If you "zmodload
> zsh/system"
> there's a sysread command which is more useful for lower level operations.
>
> pws
>

[-- Attachment #2: Type: text/html, Size: 1965 bytes --]

^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: read string containing null
  2021-10-21 13:08 ` Peter Stephenson
  2021-10-21 13:30   ` Pier Paolo Grassi
@ 2021-10-22  9:05   ` Stephane Chazelas
  2021-10-23  0:04     ` Bart Schaefer
  1 sibling, 1 reply; 5+ messages in thread
From: Stephane Chazelas @ 2021-10-22  9:05 UTC (permalink / raw)
  To: Peter Stephenson; +Cc: Pier Paolo Grassi, Zsh-Users List

2021-10-21 14:08:41 +0100, Peter Stephenson:
[...]
> If you're not trying to read a line, don't use read.  If you "zmodload zsh/system"
> there's a sysread command which is more useful for lower level operations.
[...]

Yes, though sysread does only one read() of a fixed size (8KiB
be default), so you'd need to call it in a loop.

Other options are:

cmd | LC_ALL=C read -u0 -k999999999 var

Though it allocates as much memory.

And there's:

var=$(cmd; print .); var=${var%.}

To get the contents of a regular file into a variable, there's also

zmodload zsh/mapfile
var=$mapfile[path/to/file]

-- 
Stephane


^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: read string containing null
  2021-10-22  9:05   ` Stephane Chazelas
@ 2021-10-23  0:04     ` Bart Schaefer
  0 siblings, 0 replies; 5+ messages in thread
From: Bart Schaefer @ 2021-10-23  0:04 UTC (permalink / raw)
  To: Pier Paolo Grassi, Zsh-Users List

On Fri, Oct 22, 2021 at 2:05 AM Stephane Chazelas <stephane@chazelas.org> wrote:
>
> Other options are:
>
> var=$(cmd; print .); var=${var%.}

Also with zsh/mapfile:

() { var=$mapfile[$1] } =(cmd)


^ permalink raw reply	[flat|nested] 5+ messages in thread

end of thread, other threads:[~2021-10-23  0:05 UTC | newest]

Thread overview: 5+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2021-10-21 12:35 read string containing null Pier Paolo Grassi
2021-10-21 13:08 ` Peter Stephenson
2021-10-21 13:30   ` Pier Paolo Grassi
2021-10-22  9:05   ` Stephane Chazelas
2021-10-23  0:04     ` Bart Schaefer

Code repositories for project(s) associated with this inbox:

	https://git.vuxu.org/mirror/zsh/

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).