I'm creating a script where multiple outputs from background processes are captured through while-read loop. However, it only works if I capture output directly to while loop. Trimmed down version of script is below where subscribe_audio_v1 does not work. I'm guessing its because of subshell that gets created during |
however the same pipe works in v2 of function.
#!/bin/sh
# This version doesn't work.
function subscribe_audio_v1() {
pactl subscribe | awk '/change/{print "event_audio_v1"}'
}
# This version works
function subscribe_audio_v2() {
pactl subscribe | while read -r x
do
case "$x" in
*change*) echo event_audio_v2 ;;
esac
done
}
function event_container() {
subscribe_audio_v1 &
subscribe_audio_v2 &
}
function loop() {
while read -r x
do
case $x in
event_audio*) echo "Working on ${x}..." ;;
esac
done
}
event_container | loop
Edit: I'm trying to circumvent default full buffer allocation of pipe process with a new descriptor. This makes both v1 and v2 function print their string however I'm unable to capture that output as input for loop function
exec 3>&1
event_container >&3
loop <&3
I'm creating a script where multiple outputs from background processes are captured through while-read loop. However, it only works if I capture output directly to while loop. Trimmed down version of script is below where subscribe_audio_v1 does not work. I'm guessing its because of subshell that gets created during |
however the same pipe works in v2 of function.
#!/bin/sh
# This version doesn't work.
function subscribe_audio_v1() {
pactl subscribe | awk '/change/{print "event_audio_v1"}'
}
# This version works
function subscribe_audio_v2() {
pactl subscribe | while read -r x
do
case "$x" in
*change*) echo event_audio_v2 ;;
esac
done
}
function event_container() {
subscribe_audio_v1 &
subscribe_audio_v2 &
}
function loop() {
while read -r x
do
case $x in
event_audio*) echo "Working on ${x}..." ;;
esac
done
}
event_container | loop
Edit: I'm trying to circumvent default full buffer allocation of pipe process with a new descriptor. This makes both v1 and v2 function print their string however I'm unable to capture that output as input for loop function
exec 3>&1
event_container >&3
loop <&3
Share
Improve this question
edited Mar 17 at 10:36
decepticlown
asked Mar 16 at 19:53
decepticlowndecepticlown
112 bronze badges
11
|
Show 6 more comments
1 Answer
Reset to default -1First capture output of all background processes:
data=$( event_container )
Than process it:
loop <<< "$data"
event_audio_v1
andevent_audio_v2
respectively. Function loop captures it and prints the output. But it only works for v2. – decepticlown Commented Mar 16 at 20:59fflush()
in awk? – jhnc Commented Mar 16 at 21:04