質問

The following code:

#!/bin/bash -x
mkfifo pipe 2>/dev/null
tee pipe >/dev/null &
cat pipe

produces no output when run as follows:

$ echo "hi" | ./test.sh
+ mkfifo pipe
+ cat pipe
+ tee pipe
$

Why?! I would expect tee to copy stdin to the named pipe (and /dev/null), and then cat to copy the contents of the named pipe to stdout. Why doesn't it work?!

I'm trying to write a bigger bash script and I really need the tee in there, with something else in the place of /dev/null. I narrowed down the unexpected behaviour to the example above.

役に立ちましたか?

解決

when you background a process its standard input will be set to /dev/null

#!/bin/bash -x
mkfifo pipe 2>/dev/null
cat - | tee pipe >/dev/null &
cat pipe

So you need to specify you want the stdin of the parent, in your case the pipe between echo and ./test.sh

ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top