Bash 管道破裂的过程替代
我今天做了一件傻事:Bash 管道破裂的过程替代,bash,pipe,io-redirection,command-substitution,Bash,Pipe,Io Redirection,Command Substitution,我今天做了一件傻事: read x <( ps -fu $LOGNAME | grep ' /usr/bin/ps$' ) read x进程替换将标准输出重定向到read。没有任何东西进入到grep的管道中 因此,grep正在耐心地等待和阻塞,而命令链永远不会结束 我是这样做的: read x pid x <<< "$( ps -fu $LOGNAME | grep ' /usr/bin/ps$' )" read x pid x如果将read x替换为cat,运行需要
read x <( ps -fu $LOGNAME | grep ' /usr/bin/ps$' )
read x进程替换将标准输出重定向到read。没有任何东西进入到grep
的管道中
因此,grep
正在耐心地等待和阻塞,而命令链永远不会结束
我是这样做的:
read x pid x <<< "$( ps -fu $LOGNAME | grep ' /usr/bin/ps$' )"
read x pid x如果将read x
替换为cat
,运行需要多长时间
问题是您执行了如下命令:
read x /dev/fd/63
(正如您通过运行echo read x所看到的,抱歉;这是一个错误的分析。我认为,请查看我的答案,其中一个更好。这就是我为什么要发帖的原因。有人会学到一些东西,可能就是我。“现在嘲笑我是完全正确的”XD
$ read x <( ps -fu $LOGNAME | grep ' /usr/bin/ps$' )
asc def
-bash: read: `/dev/fd/63': not a valid identifier
$
$ read x < <( ps -fu $LOGNAME | grep ' /usr/bin/ps' )
$ echo $x
501 16166 16164 0 7:50AM ttys000 0:00.00 grep /usr/bin/ps
$
while read x
do
whatever -with "$x"
done < <(ps -fu $LOGNAME | grep ' /usr/bin/ps')