Ari Lotter
Ari Lotter

Reputation: 615

How can I run a non-blocking bash command and block on its output?

I'm trying to write a script that will run a program, wait for a certain output from the program, then continue execution (and leave the program running.) My current code doesn't seem to ever output anything, sed never returns true. This echos "Peerflix started" but that's it.

exec 3< <(peerflix $1 -p 8888)
echo "Peerflix started."
sed '/server$/q' <&3
echo 'Matched'

Upvotes: 3

Views: 2420

Answers (1)

Idan Arye
Idan Arye

Reputation: 12603

Use pipes!

Use mkfifo to created a pipe and stream the program's output to it in a non-blocking command. Then use your blocking sed to read from that pipe.

Something like(did not test - I don't have peerflix):

mkfifo myfifo
peerflix $1 -p 8888 > myfifo &
echo "Peerflix started."
sed '/server$/q' myfifo
echo 'Matched'
rm myfifo

Upvotes: 1

Related Questions