zsh-users
 help / color / mirror / code / Atom feed
* read stdin in variable howto ?
@ 2006-01-31  9:28 Marc Chantreux
       [not found] ` <ef88cc6b0601310206w26e9a277o5ad42bda342c795a@mail.gmail.com>
                   ` (2 more replies)
  0 siblings, 3 replies; 9+ messages in thread
From: Marc Chantreux @ 2006-01-31  9:28 UTC (permalink / raw)
  To: zsh-users

hi all,

is there a way to use $(<) to read stdin ? it will be great for write
something like

for arg ( ${(f)"$(<)"} $@ ) treat_arg $arg

regards
mc


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

* Re: read stdin in variable howto ?
       [not found] ` <ef88cc6b0601310206w26e9a277o5ad42bda342c795a@mail.gmail.com>
@ 2006-01-31 10:11   ` Marc Chantreux
  0 siblings, 0 replies; 9+ messages in thread
From: Marc Chantreux @ 2006-01-31 10:11 UTC (permalink / raw)
  To: Marcus Fritzsch, zsh-users

> What about using the following:
>    ,-----
>    | while :; do read line; test -n "$line" || break; echo $line; done
>    `-----

for the moment, i use :

typeset stdin
read -d\\0 stdin
logins=( ${(f)stdin})

but it's not as elegant as a ${(f)"$(<)"} would be.

thanks for help
mc


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

* Re: read stdin in variable howto ?
  2006-01-31  9:28 read stdin in variable howto ? Marc Chantreux
       [not found] ` <ef88cc6b0601310206w26e9a277o5ad42bda342c795a@mail.gmail.com>
@ 2006-01-31 10:36 ` Artur Penttinen
  2006-01-31 11:09   ` Marc Chantreux
  2006-01-31 12:07 ` Vincent Lefevre
  2 siblings, 1 reply; 9+ messages in thread
From: Artur Penttinen @ 2006-01-31 10:36 UTC (permalink / raw)
  To: marc.chantreux; +Cc: zsh-users

>hi all,
>
>is there a way to use $(<) to read stdin ? it will be great for write
>something like
>
>for arg ( ${(f)"$(<)"} $@ ) treat_arg $arg

  # for arg in ${(@f)"$(cat)"}; do print "ARG: $arg"; done
  test pd
  pod
  ^D
  ARG: test pd
  ARG: pod
  #

-- 
wbw, artur


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

* Re: read stdin in variable howto ?
  2006-01-31 10:36 ` Artur Penttinen
@ 2006-01-31 11:09   ` Marc Chantreux
  0 siblings, 0 replies; 9+ messages in thread
From: Marc Chantreux @ 2006-01-31 11:09 UTC (permalink / raw)
  To: zsh-users

le 31/01/2006,
Artur Penttinen nous écrivait :
>   # for arg in ${(@f)"$(cat)"}; do print "ARG: $arg"; done

UUOC ;). i mean : isn't it sad to use cat for that ?

thanks anyway.

regards
mc


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

* Re: read stdin in variable howto ?
  2006-01-31  9:28 read stdin in variable howto ? Marc Chantreux
       [not found] ` <ef88cc6b0601310206w26e9a277o5ad42bda342c795a@mail.gmail.com>
  2006-01-31 10:36 ` Artur Penttinen
@ 2006-01-31 12:07 ` Vincent Lefevre
  2006-01-31 12:09   ` Vincent Lefevre
  2 siblings, 1 reply; 9+ messages in thread
From: Vincent Lefevre @ 2006-01-31 12:07 UTC (permalink / raw)
  To: zsh-users

On 2006-01-31 10:28:13 +0100, Marc Chantreux wrote:
> is there a way to use $(<) to read stdin ? it will be great for write
> something like
> 
> for arg ( ${(f)"$(<)"} $@ ) treat_arg $arg

$(</dev/stdin) or $(</dev/fd/0)

but I don't know if this is portable.

-- 
Vincent Lefèvre <vincent@vinc17.org> - Web: <http://www.vinc17.org/>
100% accessible validated (X)HTML - Blog: <http://www.vinc17.org/blog/>
Work: CR INRIA - computer arithmetic / SPACES project at LORIA


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

* Re: read stdin in variable howto ?
  2006-01-31 12:07 ` Vincent Lefevre
@ 2006-01-31 12:09   ` Vincent Lefevre
  2006-01-31 13:45     ` Marc Chantreux
  0 siblings, 1 reply; 9+ messages in thread
From: Vincent Lefevre @ 2006-01-31 12:09 UTC (permalink / raw)
  To: zsh-users

On 2006-01-31 13:07:44 +0100, Vincent Lefevre wrote:
> On 2006-01-31 10:28:13 +0100, Marc Chantreux wrote:
> > is there a way to use $(<) to read stdin ? it will be great for write
> > something like
> > 
> > for arg ( ${(f)"$(<)"} $@ ) treat_arg $arg
> 
> $(</dev/stdin) or $(</dev/fd/0)
> 
> but I don't know if this is portable.

$(<&0) also works.

-- 
Vincent Lefèvre <vincent@vinc17.org> - Web: <http://www.vinc17.org/>
100% accessible validated (X)HTML - Blog: <http://www.vinc17.org/blog/>
Work: CR INRIA - computer arithmetic / SPACES project at LORIA


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

* Re: read stdin in variable howto ?
  2006-01-31 12:09   ` Vincent Lefevre
@ 2006-01-31 13:45     ` Marc Chantreux
  2006-01-31 14:51       ` Stephane Chazelas
  0 siblings, 1 reply; 9+ messages in thread
From: Marc Chantreux @ 2006-01-31 13:45 UTC (permalink / raw)
  To: zsh-users

le 31/01/2006,
Vincent Lefevre nous écrivait :
> $(<&0) also works.

exactly kind of answer i expected! thanks a lot Vincent!

mc


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

* Re: read stdin in variable howto ?
  2006-01-31 13:45     ` Marc Chantreux
@ 2006-01-31 14:51       ` Stephane Chazelas
  2006-01-31 21:10         ` Marc Chantreux
  0 siblings, 1 reply; 9+ messages in thread
From: Stephane Chazelas @ 2006-01-31 14:51 UTC (permalink / raw)
  To: zsh-users

On Tue, Jan 31, 2006 at 02:45:48PM +0100, Marc Chantreux wrote:
> le 31/01/2006,
> Vincent Lefevre nous écrivait :
> > $(<&0) also works.
> 
> exactly kind of answer i expected! thanks a lot Vincent!
[...]

But it does the same thing as $(cat) (it does call cat after a
pipe and a fork through the $NULLCMD mechanism), contrary to $(<
/dev/stdin) which uses the $(< file) special operator.

In $(<&0), it's command substitution, and $NULLCMD <&0. So the
shell forks a "cat" process whose stdout is set to a pipe. the
parent process reads the other end of the pipe to build the
string.

In $(< /dev/stdin), the shell reads the /dev/stdin file by
itself in the current process (no fork, no pipe). /dev/stdin (or
/dev/fd/0 or /proc/self/fd/0) is a special feature of some
operating systems, which on some systems (like Solaris) makes
that an open("/dev/stdin") is equivalent to a dup(0), and on
others (like Linux) opens the same resource as the one pointed
to by the fd 0 of the process (beware that some shells like bash
or ksh have a confusing feature where they emulate that OS
mechanism (well in the Solaris way) (in some circumstancies)).

Note that in both cases, the trailing empty lines are removed.
That's a bug/feature common to all the Bourne like shells.

-- 
Stéphane


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

* Re: read stdin in variable howto ?
  2006-01-31 14:51       ` Stephane Chazelas
@ 2006-01-31 21:10         ` Marc Chantreux
  0 siblings, 0 replies; 9+ messages in thread
From: Marc Chantreux @ 2006-01-31 21:10 UTC (permalink / raw)
  To: zsh-users

thanks for detailled explanations, Stephane. Really interesting for me.

regards,
mc


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

end of thread, other threads:[~2006-01-31 21:08 UTC | newest]

Thread overview: 9+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2006-01-31  9:28 read stdin in variable howto ? Marc Chantreux
     [not found] ` <ef88cc6b0601310206w26e9a277o5ad42bda342c795a@mail.gmail.com>
2006-01-31 10:11   ` Marc Chantreux
2006-01-31 10:36 ` Artur Penttinen
2006-01-31 11:09   ` Marc Chantreux
2006-01-31 12:07 ` Vincent Lefevre
2006-01-31 12:09   ` Vincent Lefevre
2006-01-31 13:45     ` Marc Chantreux
2006-01-31 14:51       ` Stephane Chazelas
2006-01-31 21:10         ` Marc Chantreux

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).