From: Mack Wallace <mackbw@mapinternet.com>
To: Fans of the OS Plan 9 from Bell Labs <9fans@9fans.net>
Subject: Re: [9fans] rc scripts, how to get spaces in array elements generated by command output?
Date: Sat, 5 Jan 2019 20:05:42 -0500 [thread overview]
Message-ID: <589A07DC-601C-4BBA-BD38-F5747B19CF70@mapinternet.com> (raw)
In-Reply-To: <20190105235228.GA21088@alice>
[-- Attachment #1: Type: text/plain, Size: 2289 bytes --]
Thank you Anthony!
I had thought that IFS in Plan 9 was not the way to go after reading from ‘Rc - The Plan 9 Shell’
“IFS is no longer used, except in the one case where it was indispensable:
converting command output into argument lists during command substitution.”
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’ve ended up doing:
headrec = `{read $1 | sed ’s/^/,/; s/\\”/☹/g; s/,”([^”]*)”/,☺\1☻/g; s/,”/,☺/; :MC; s/☺([^☻]*),([^☻]*)/☺\1☯\2/g; tMC; s/^,//; s/,/♪/g’}
oldifs = $ifs
ifs = ♪
headers = `{echo $headrec | sed ‘/s/☹/\\”/g; s/(☺|☻)/“/g; s/☯/,/g’}
ifs = $oldifs
I’m not sure if there is a cleaner way to do this - but it gets the array of headers how they should be.
I’ll 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 = `{echo some_string | sed ’s/x/y/g’}
>>
>> If the output was ‘hello world’, 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 = `{echo -n hello world}
> % echo $#x
> 2
> % echo $x(1)
> hello
> % echo $x(2)
> world
>
> Using only newline as the separator yields:
>
> % ifs = '
> ' # this is a newline character
> % y = `{echo -n hello world}
> % echo $#y
> 1
> % echo $y(1)
> hello world
>
> You might want to use a comma instead:
>
> % ifs = ,
> % z = `{echo -n hello world,good luck}
> % echo $#z
> 2
> % echo $z(1)
> hello world
> % echo $z(2)
> good luck
>
> Cheers,
> Anthony
>
>
[-- Attachment #2: Type: text/html, Size: 8541 bytes --]
next prev parent reply other threads:[~2019-01-06 1:05 UTC|newest]
Thread overview: 4+ messages / expand[flat|nested] mbox.gz Atom feed top
2019-01-05 21:13 Mack Wallace
2019-01-05 23:52 ` Anthony Martin
2019-01-06 1:05 ` Mack Wallace [this message]
2019-01-06 14:34 ` Ethan Gardener
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=589A07DC-601C-4BBA-BD38-F5747B19CF70@mapinternet.com \
--to=mackbw@mapinternet.com \
--cc=9fans@9fans.net \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
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).