mus*_*hil 5 bash process-substitution
如果我跑
$#/bin/bash
for i in `seq 5`; do
exec 3> >(sed -e "s/^/$i: /"; echo "$i-")
echo foo >&3
echo bar >&3
exec 3>&-
done
Run Code Online (Sandbox Code Playgroud)
那么结果是不同步的;它可能是这样的:
1: foo
1: bar
2: foo
2: bar
1-
3: foo
3: bar
2-
3-
4: foo
5: foo
4: bar
5: bar
4-
5-
Run Code Online (Sandbox Code Playgroud)
>(...)在进行下一次迭代之前,如何确保流程替换已完成?
sleep 0.1在exec 3>&-帮助之后插入,但它不优雅,效率低下,并且不能保证总是有效。
编辑:这个例子可能看起来很傻,但它只是为了说明。我正在做的是在循环中读取输入流,将每一行提供给一个在循环中偶尔会改变的进程。在代码中更容易解释:
# again, simplified for illustration
while IFS= read line; do
case $line in
@*)
exec 3>&-
filename=${line:1}
echo "starting $filename"
exec 3> >(sort >"$filename"; echo "finished $filename")
;;
*)
echo "$line" >&3
;;
esac
done
exec 3>&-
Run Code Online (Sandbox Code Playgroud)
以下在 bash 4 中使用coprocesses 工作:
#!/bin/bash
fd_re='^[0-9]+$'
cleanup_and_wait() {
if [[ ${COPROC[1]} =~ $fd_re ]] ; then
eval "exec ${COPROC[1]}<&-"
echo "waiting for $filename to finish" >&2
wait $COPROC_PID
fi
}
while IFS= read -r line; do
case $line in
@*)
cleanup_and_wait
filename=${line:1}
echo "starting $filename" >&2
coproc { sort >"$filename"; echo "Finished with $filename" >&2; }
;;
*)
printf '%s\n' "$line" >&${COPROC[1]}
;;
esac
done
cleanup_and_wait
Run Code Online (Sandbox Code Playgroud)
对于之前版本的 bash,可以使用命名管道代替:
cleanup_and_wait() {
if [[ $child_pid ]] ; then
exec 4<&-
echo "waiting for $filename to finish" >&2
wait $child_pid
fi
}
# this is a bit racy; without a force option to mkfifo,
# however, the race is unavoidable
fifo_name=$(mktemp -u -t fifo.XXXXXX)
if ! mkfifo "$fifo_name" ; then
echo "Someone else may have created our temporary FIFO before we did!" >&2
echo "This can indicate an attempt to exploit a race condition as a" >&2
echo "security vulnarability and should always be tested for." >&2
exit 1
fi
# ensure that we clean up even on unexpected exits
trap 'rm -f "$fifo_name"' EXIT
while IFS= read -r line; do
case $line in
@*)
cleanup_and_wait
filename=${line:1}
echo "starting $filename" >&2
{ sort >"$filename"; echo "finished with $filename" >&2; } <"$fifo_name" &
child_pid=$!
exec 4>"$fifo_name"
;;
*)
printf '%s\n' "$line" >&4
;;
esac
done
cleanup_and_wait
Run Code Online (Sandbox Code Playgroud)
一些注意事项:
printf '%s\n' "$line"比使用更安全echo "$line";-e例如,如果一行仅包含,某些版本的echo将不处理它。很简单,只需将所有内容都输入到 cat 中即可。
#!/bin/bash
for i in `seq 5`; do
{
exec 3> >(sed -e "s/^/$i: /"; echo "$i-")
echo foo >&3
echo bar >&3
exec 3<&-
}|cat
done
Run Code Online (Sandbox Code Playgroud)
这是输出:
1: foo
1: bar
1-
2: foo
2: bar
2-
3: foo
3: bar
3-
4: foo
4: bar
4-
5: foo
5: bar
5-
Run Code Online (Sandbox Code Playgroud)