admin管理员组文章数量:1390887
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"
本文标签: while loopHow to read piped output produced in background subshellStack Overflow
版权声明:本文标题:while loop - How to read piped output produced in background subshell - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744587510a2614282.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
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