help-bash
[Top][All Lists]
Advanced

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

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


From: Peng Yu
Subject: [Help-bash] How to get stdin from the parent process?
Date: Wed, 21 Feb 2018 22:31:16 -0600

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

I come up with the following solution using a fifo.

$ cat main.sh
#!/usr/bin/env bash
# vim: set noexpandtab tabstop=2:

fifo=$(mktemp -u)
mkfifo "$fifo"

cat < /dev/stdin > "$fifo" &

(
    while read -r i
    do
        echo "$i"
    done < "$fifo"
) &
wait
$ seq 3 | ./main.sh
1
2
3

Does anybody have any better way to solve this problem? Thanks.

-- 
Regards,
Peng



reply via email to

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