From mboxrd@z Thu Jan 1 00:00:00 1970 From: Mack Wallace Content-Type: multipart/alternative; boundary="Apple-Mail=_CE8E775C-F94B-42A0-BD65-465FE0384B70" Mime-Version: 1.0 (Mac OS X Mail 12.1 \(3445.101.1\)) Date: Sat, 5 Jan 2019 20:05:42 -0500 References: <72B9C1DF-7927-4F58-9529-3DC122DEF47C@mapinternet.com> <20190105235228.GA21088@alice> To: Fans of the OS Plan 9 from Bell Labs <9fans@9fans.net> In-Reply-To: <20190105235228.GA21088@alice> Message-Id: <589A07DC-601C-4BBA-BD38-F5747B19CF70@mapinternet.com> Subject: Re: [9fans] rc scripts, how to get spaces in array elements generated by command output? Topicbox-Message-UUID: f2b57fd2-ead9-11e9-9d60-3106f5b1d025 --Apple-Mail=_CE8E775C-F94B-42A0-BD65-465FE0384B70 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset=utf-8 Thank you Anthony! I had thought that IFS in Plan 9 was not the way to go after reading = from =E2=80=98Rc - The Plan 9 Shell=E2=80=99=20 =E2=80=9CIFS is no longer used, except in the one case where it was = indispensable: converting command output into argument lists during command = substitution.=E2=80=9D The document then followed about avoiding a UNIX security hole, and = lacking examples was not sure what this meant. Of course, in retrospect, = it means exactly what I want to do. So what I=E2=80=99ve ended up doing: headrec =3D `{read $1 | sed =E2=80=99s/^/,/; s/\\=E2=80=9D/=E2=98=B9/g; = s/,=E2=80=9D([^=E2=80=9D]*)=E2=80=9D/,=E2=98=BA\1=E2=98=BB/g; = s/,=E2=80=9D/,=E2=98=BA/; :MC; s/=E2=98=BA([^=E2=98=BB]*),([^=E2=98=BB]*)/= =E2=98=BA\1=E2=98=AF\2/g; tMC; s/^,//; s/,/=E2=99=AA/g=E2=80=99} oldifs =3D $ifs ifs =3D =E2=99=AA headers =3D `{echo $headrec | sed =E2=80=98/s/=E2=98=B9/\\=E2=80=9D/g; = s/(=E2=98=BA|=E2=98=BB)/=E2=80=9C/g; s/=E2=98=AF/,/g=E2=80=99} ifs =3D $oldifs I=E2=80=99m not sure if there is a cleaner way to do this - but it gets = the array of headers how they should be. I=E2=80=99ll look at your other message. Thanks, Mack > On Jan 5, 2019, at 6:52 PM, Anthony Martin wrote: >=20 > Mack Wallace once said: >> My question: Is there a way to take the string output of a command = that >> contains spaces and make it a single element of an array in rc = script? >>=20 >> [...] >>=20 >> However, if I am receiving output from sed that contains spaces from >> the following script line >>=20 >> string_var =3D `{echo some_string | sed =E2=80=99s/x/y/g=E2=80=99} >>=20 >> If the output was =E2=80=98hello world=E2=80=99, string_var would = become a two >> element array which >> echo $some_string(1)=20 >> echo $some_string(2) >>=20 >> Would output=20 >> hello >> world >=20 > You need to change the input field separator, $ifs. >=20 > The default value is any whitespace character: >=20 > % x =3D `{echo -n hello world} > % echo $#x > 2 > % echo $x(1) > hello > % echo $x(2) > world >=20 > Using only newline as the separator yields: >=20 > % ifs =3D ' > ' # this is a newline character > % y =3D `{echo -n hello world} > % echo $#y > 1 > % echo $y(1) > hello world >=20 > You might want to use a comma instead: >=20 > % ifs =3D , > % z =3D `{echo -n hello world,good luck} > % echo $#z > 2 > % echo $z(1) > hello world > % echo $z(2) > good luck >=20 > Cheers, > Anthony >=20 >=20 --Apple-Mail=_CE8E775C-F94B-42A0-BD65-465FE0384B70 Content-Transfer-Encoding: quoted-printable Content-Type: text/html; charset=utf-8 Thank= you Anthony!

I had = thought that IFS in Plan 9 was not the way to go after reading from = =E2=80=98Rc - The Plan 9 Shell=E2=80=99 

     =E2=80=9CIFS is no = longer used, except in the one case where it was = indispensable:
      converting = command output into argument lists during command = substitution.=E2=80=9D

The document then followed about avoiding a UNIX security = hole, and lacking examples was not sure what this meant. Of course, in = retrospect, it means exactly what I want to do.

So what I=E2=80=99ve ended up = doing:

headrec = =3D `{read $1 | sed =E2=80=99s/^/,/; s/\\=E2=80=9D/=E2=98=B9/g; = s/,=E2=80=9D([^=E2=80=9D]*)=E2=80=9D/,=E2=98=BA\1=E2=98=BB/g; = s/,=E2=80=9D/,=E2=98=BA/; = :MC; s/=E2=98=BA([^=E2=98=BB]*),([^=E2=98=BB]*)/=E2=98=BA\1=E2=98=AF\2/g; tMC; = s/^,//; s/,/=E2=99=AA/g=E2=80=99}
oldifs =3D = $ifs
ifs =3D =E2=99=AA
headers =3D `{echo $headrec | sed =E2=80=98/s/=E2=98=B9/\= \=E2=80=9D/g; s/(=E2=98=BA|=E2=98=BB)/=E2=80=9C/g; = s/=E2=98=AF/,/g=E2=80=99}
ifs =3D = $oldifs

I=E2=80=99m not sure if there is a cleaner way to do this - = but it gets the array of headers how they should = be.

I=E2=80=99ll look at your other = message.

Thanks,

Mack




On = Jan 5, 2019, at 6:52 PM, Anthony Martin <ality@pbrane.org> = wrote:

Mack Wallace <mackbw@mapinternet.com> once said:
My question: Is there a = way to take the string output of a command that
contains = spaces and make it a single element of an array in rc script?

[...]

However, if = I am receiving output from sed that contains spaces from
the= following script line

string_var =3D = `{echo some_string | sed =E2=80=99s/x/y/g=E2=80=99}

If the output was =E2=80=98hello world=E2=80=99, string_var = would become a two
element array which
echo = $some_string(1)
echo $some_string(2)

Would output
hello
world

You need to change the input = field separator, $ifs.

The default value is = any whitespace character:

% x =3D = `{echo -n hello world}
% echo $#x
2
= % echo $x(1)
hello
% echo = $x(2)
world

Using only newline as the separator yields:

= % ifs =3D '
' # this is a newline = character
% y =3D `{echo -n hello world}
= % echo $#y
1
% echo = $y(1)
hello world

You might want to use a comma instead:

= % ifs =3D ,
% z =3D `{echo -n hello = world,good luck}
% echo $#z
2
= % echo $z(1)
hello world
% echo = $z(2)
good luck

Cheers,
 Anthony



= --Apple-Mail=_CE8E775C-F94B-42A0-BD65-465FE0384B70--