不支持流程替换<(...)吗? [英] Doesn't sh support process substitution <(...)?
问题描述
在Centos 6机器上,此方法有效:
On a Centos 6 machine, this works:
bash -c 'if grep -qP --line-buffered ".+" <(tail -n 1000 -F catalina.out) ; then echo "yes"; fi'
这不是:
sh -c 'if grep -qP --line-buffered ".+" <(tail -n 1000 -F catalina.out) ; then echo "yes"; fi'
我得到:
sh: -c: line 0: syntax error near unexpected token `('
sh: -c: line 0: `if grep -qP --line-buffered ".+" <(tail -n 1000 -F catalina.out) ; then echo "yes"; fi'
不要紧记grep和尾巴.问题出在进程替换上:<(...)
Nevermind the grep and tail. The problem is with the process substitution thingy: <(...)
有人可以告诉我sh在这里有什么不同吗?
Can someone tell me what sh does differently here?
感谢您的回答!
在使用 capistrano 进行部署时出现了问题.它默认使用 sh ,但是我现在将其更改为 bash .
我无法进行常规配管的原因是,使用tail -F | grep -q --line-buffered
时,grep不会在匹配后立即退出.必须像echo "" >> catalina.out
这样的文件再进行一次编辑,在我的情况下这是不可接受的.
The problem arose while using capistrano for deployments. It defaults to using sh but I changed that to bash now.
The reason I couldn't do the normal piping is that when using tail -F | grep -q --line-buffered
, grep won't exit immediately after a match. There has to be one more edit to the file like echo "" >> catalina.out
and this was not acceptable in my situation.
推荐答案
BASH仅支持语法<(...)
.
The syntax <(...)
is only supported by BASH.
对于任何POSIX shell,请使用以下方法:
For any POSIX shell, use this approach:
sh -c 'tail -n 1000 -F catalina.out | if grep -qP --line-buffered ".+" ; then ...'
即用管道将stdin重定向移到if
的前面. if
会将标准输入传递到grep
.
i.e. move the stdin redirection in front of the if
with a pipe. The if
will pass stdin on to the grep
.
if tail ...| grep
不起作用,因为if
不能看到它是then
/fi
,因为管道将进程分开.
if tail ...| grep
won't work since the if
won't be able to see it's then
/fi
because the pipe separates processes.
这篇关于不支持流程替换<(...)吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!