On Tue, Apr 16, 2024 at 3:56 AM Andreas Schwab <sch...@suse.de> wrote:
>
> On Apr 16 2024, Carl Edquist wrote:
>
> > Well, you _can_ shovel binary data too: (*)
> >
> >       while IFS= read -rd '' X; do printf '%s\0' "$X"; done
> >
> > and use that pattern to make a shell-only version of tee(1) (and I suppose
> > paste(1)).  Binary data doesn't work if you're reading newline-terminated
> > records, because you cannot store the NUL character in a shell
> > variable. But you can delimit your records on NULs, and use printf to
> > reproduce them.
>
> Though that will likely add a spurious null at EOF.

Just wouldn't copy over whatever might have followed the final null
byte, if we're not talking about null-terminated data.

printf_format='%s\x00'
while
  IFS='' read -r -d '' X ||
    {
      [[ -n ${X} ]] &&
        {
          printf_format='%s'
          true
        }
      #
    }
  #
do
  printf -- "${printf_format}" "${X}"
done

Might've gotten lucky with all those .so files ending in a null byte
for whatever reason.

There's no way to force this to give you the equivalent of sized
buffers. 'read -N' obviously has the same problem of trying to store
the null character in a variable. So, if you're trying to run this on
a huge text file, you're going to end up trying to shove that entire
file into a variable.

Reply via email to