zsh-users
 help / color / mirror / code / Atom feed
* Reading output into variables
@ 2014-04-07 14:20 Yuri D'Elia
  2014-04-07 15:11 ` Peter Stephenson
                   ` (3 more replies)
  0 siblings, 4 replies; 5+ messages in thread
From: Yuri D'Elia @ 2014-04-07 14:20 UTC (permalink / raw)
  To: zsh-users

Maybe a stupid question, but is there a way to read the output of a
program and split into variables _conveniently_ using the IFS?

To clarify, I would the something as convenient as:

program | read a b

minus the subshell.
Note that I actually did the following:

program | { read a b; hooray }

when I could, so this "solution" doesn't count.
Bonus points if that's something that would also work in bash.


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

* Re: Reading output into variables
  2014-04-07 14:20 Reading output into variables Yuri D'Elia
@ 2014-04-07 15:11 ` Peter Stephenson
  2014-04-07 15:16 ` TJ Luoma
                   ` (2 subsequent siblings)
  3 siblings, 0 replies; 5+ messages in thread
From: Peter Stephenson @ 2014-04-07 15:11 UTC (permalink / raw)
  To: zsh-users

On Mon, 07 Apr 2014 16:20:20 +0200
Yuri D'Elia <wavexx@thregr.org> wrote:
> Maybe a stupid question, but is there a way to read the output of a
> program and split into variables _conveniently_ using the IFS?
> 
> To clarify, I would the something as convenient as:
> 
> program | read a b
> 
> minus the subshell.

I'm not quite sure what you're objecting to.  If "program" is an
external programme (but you can call it a program if you want :-)) this
is already handled efficiently because the shell exec's it directly from
the subshell, so there's only a single fork.

Beyond that, there are other possibilities, but none of them stand
out as obviously neater or more efficient, given the facts above.

pws


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

* Re: Reading output into variables
  2014-04-07 14:20 Reading output into variables Yuri D'Elia
  2014-04-07 15:11 ` Peter Stephenson
@ 2014-04-07 15:16 ` TJ Luoma
  2014-04-07 15:44 ` Bart Schaefer
  2014-04-08 11:32 ` Martin Vaeth
  3 siblings, 0 replies; 5+ messages in thread
From: TJ Luoma @ 2014-04-07 15:16 UTC (permalink / raw)
  To: Yuri D'Elia; +Cc: Zsh-Users List

I don't know of a way to split it into separate variables exactly, but
you could use

FOO=($(program))

and then

"$FOO[1]" would be "a" and "$FOO[2]" would be b

Tj




On Mon, Apr 7, 2014 at 10:20 AM, Yuri D'Elia <wavexx@thregr.org> wrote:
> Maybe a stupid question, but is there a way to read the output of a
> program and split into variables _conveniently_ using the IFS?
>
> To clarify, I would the something as convenient as:
>
> program | read a b
>
> minus the subshell.
> Note that I actually did the following:
>
> program | { read a b; hooray }
>
> when I could, so this "solution" doesn't count.
> Bonus points if that's something that would also work in bash.
>


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

* Re: Reading output into variables
  2014-04-07 14:20 Reading output into variables Yuri D'Elia
  2014-04-07 15:11 ` Peter Stephenson
  2014-04-07 15:16 ` TJ Luoma
@ 2014-04-07 15:44 ` Bart Schaefer
  2014-04-08 11:32 ` Martin Vaeth
  3 siblings, 0 replies; 5+ messages in thread
From: Bart Schaefer @ 2014-04-07 15:44 UTC (permalink / raw)
  To: zsh-users

On Apr 7,  4:20pm, Yuri D'Elia wrote:
}
} Maybe a stupid question, but is there a way to read the output of a
} program and split into variables _conveniently_ using the IFS?

Like Peter, I'm a little confused about what you're asking.

A basic constraint of the way that I/O works in unix-like systems is
that you have to use either the filesystem or a separate process to
"read the output" of anything without danger of deadlock.  So when
you ask for ...

} program | read a b
} 
} minus the subshell.

... it would seem to imply that "program" is a shell construct so it
doesn't need an extra process in the first place, which leaves a temp
file as the only answer to your question ... but that probably isn't
as "convenient" as you want.

But then you give this example ...

} program | { read a b; hooray }

... which leaves me uncertain as to what "subshell" you were talking
about in the first example.

Perhaps you mean something like

    set -- $(program)

which still uses a subshell but splits on IFS into $1 $2 etc.  Which
means you have to be done using those as program / function arguments.

} Bonus points if that's something that would also work in bash.

The above "set" solution works in bash.  In zsh you could also do

    set -A array -- $(program)

to avoid clobbering $1 et al.


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

* Re: Reading output into variables
  2014-04-07 14:20 Reading output into variables Yuri D'Elia
                   ` (2 preceding siblings ...)
  2014-04-07 15:44 ` Bart Schaefer
@ 2014-04-08 11:32 ` Martin Vaeth
  3 siblings, 0 replies; 5+ messages in thread
From: Martin Vaeth @ 2014-04-08 11:32 UTC (permalink / raw)
  To: zsh-users

Yuri D'Elia <wavexx@thregr.org> wrote:
>
> program | read a b
>
> minus the subshell.

In zsh or bash:

read a b < <(program)

In POSIX (hence also in zsh or bash):

read a b <<END
$(program)
END


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

end of thread, other threads:[~2014-04-08 15:00 UTC | newest]

Thread overview: 5+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2014-04-07 14:20 Reading output into variables Yuri D'Elia
2014-04-07 15:11 ` Peter Stephenson
2014-04-07 15:16 ` TJ Luoma
2014-04-07 15:44 ` Bart Schaefer
2014-04-08 11:32 ` Martin Vaeth

Code repositories for project(s) associated with this public 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).