help-bash
[Top][All Lists]
Advanced

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

Re: [Help-bash] How to get stdin from the parent process?


From: Pierre Gaston
Subject: Re: [Help-bash] How to get stdin from the parent process?
Date: Thu, 22 Feb 2018 09:34:14 +0200

On Thu, Feb 22, 2018 at 6:31 AM, Peng Yu <address@hidden> wrote:

> Hi,
>
> The `read` in the following example is not able to read from the stdin
> of the script.
>
> $ cat main.sh
> #!/usr/bin/env bash
> # vim: set noexpandtab tabstop=2:
>
> (
>     while read -r i
>     do
>         echo "$i"
>     done
> ) &
> wait
> $ seq 3 | ./main.sh
>
>
you can save stdin in another fd that will be inherited, then redirect
stdin explicitely :

#!/usr/bin/env bash
# vim: set noexpandtab tabstop=2:
exec 3<&0
(
    while read -r i
    do
        echo "$i"
    done
) <&3 &
wait


PS: the relevant part of the manual is:
 If a command is followed by a & and job control is not active, the default
standard input for the command is the empty file /dev/null.  Otherwise,  the
       invoked command inherits the file descriptors of the calling shell
as modified by redirections.

So interactively, where job control is enabled, stdin is not redirected


reply via email to

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