[Top][All Lists]

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: RFE: readarray "-0" (or reciprocal of printf "%s\x00" "address@hidd

From: Eduardo A . Bustamante López
Subject: Re: RFE: readarray "-0" (or reciprocal of printf "%s\x00" "address@hidden" )
Date: Fri, 23 Aug 2013 22:02:11 -0700
User-agent: Mutt/1.5.21 (2010-09-15)

> However, has any thought (or is there a way already?)
> to read in a bunch of null terminated names from
> the output of such a construct?

Well, there's not a construct to build an array like you mention, but
you can use read and a while loop like this:

while IFS= read -rd '' elem; do
  echo "<$elem>";
done < <(printf '%s\0' a b '1 2 3' $'x y \n z')

Which outputs:

<1 2 3>
<x y 

Now, in more detail:

#      .- This is used to disable leading/trailing whitespace
#      |   trimming.
#      v
while IFS= read -rd '' elem; do
#                    ^.. When you pass an empty string as a
#                        "delimiter" for read, it (read) will read
#                        until it finds a NUL character.
  echo "<$elem>";
done < <(printf '%s\0' a b '1 2 3' $'x y \n z')
#       ^.. we use process substitution ( <(...) ) instead of a
#           normal anonymous pipe in case we want to create some
#           persistent variables.

Just be careful, as written above, it requires each record to end
with a NUL byte. If there's no guarantee of a trailing NUL for each
record, you will have to test if read managed to read one last
element with no trailing NUL, it looks something like this:

while ... read -r ... something; do

[[ $something ]] && ...

Eduardo Bustamante

reply via email to

[Prev in Thread] Current Thread [Next in Thread]