COPROC: mudanças entre as edições

De Wiki Clusterlab.com.br
Ir para navegação Ir para pesquisar
Sem resumo de edição
Linha 3: Linha 3:
=Example=
=Example=
Run a "bc" in a subshell and keep it running to call multiple times from a loop without the overhead to instantiate "bc" every single iteration.
Run a "bc" in a subshell and keep it running to call multiple times from a loop without the overhead to instantiate "bc" every single iteration.
<syntaxhighlight lang=bash>
coproc bc
for i in {1..10}
do
  echo $i'*'$i >&${COPROC[1]}
  read -u ${COPROC[0]} msg
  echo $msg
done
kill $COPROC_PID
</syntaxhighlight>
OUTPUT
<pre>
1
4
9
16
25
36
49
64
81
100
</pre>
Run sed with buffering disabled, using coproc.
<syntaxhighlight lang=bash>
<syntaxhighlight lang=bash>
coproc stdbuf -o0 sed 's/.*/msg: &/'
coproc stdbuf -o0 sed 's/.*/msg: &/'
Linha 8: Linha 32:
do  
do  
   echo $i >&${COPROC[1]}
   echo $i >&${COPROC[1]}
   read -u ${COPROC[0]} rc
   read -u ${COPROC[0]} msg
   echo $rc
   echo $msg
done
done
kill $COPROC_PID
kill $COPROC_PID

Edição das 11h46min de 4 de dezembro de 2019

Articles

Example

Run a "bc" in a subshell and keep it running to call multiple times from a loop without the overhead to instantiate "bc" every single iteration.

coproc bc
for i in {1..10}
do 
  echo $i'*'$i >&${COPROC[1]}
  read -u ${COPROC[0]} msg
  echo $msg
done
kill $COPROC_PID

OUTPUT

1
4
9
16
25
36
49
64
81
100

Run sed with buffering disabled, using coproc.

coproc stdbuf -o0 sed 's/.*/msg: &/'
for i in {1..10}
do 
  echo $i >&${COPROC[1]}
  read -u ${COPROC[0]} msg
  echo $msg
done
kill $COPROC_PID

OUTPUT

msg: 1
msg: 2
msg: 3
msg: 4
msg: 5
msg: 6
msg: 7
msg: 8
msg: 9
msg: 10