Doesn't sh support process substitution <(…)?

前端 未结 3 1998
北荒
北荒 2020-12-07 02:55

On a Centos 6 machine, this works:

bash -c \'if grep -qP --line-buffered \".+\" <(tail -n 1000 -F catalina.out) ; then echo \"yes\"; fi\'
<
相关标签:
3条回答
  • 2020-12-07 03:14

    The syntax <(...) is only supported by BASH.

    For any POSIX shell, use this approach:

    sh -c 'tail -n 1000 -F catalina.out | if grep -qP --line-buffered ".+" ; then ...'
    

    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 won't work since the if won't be able to see it's then/fi because the pipe separates processes.

    0 讨论(0)
  • 2020-12-07 03:23

    Also take note that if Bash is invoked with the name sh, it tries to mimic the startup behavior of historical versions of sh as closely as possible, while conforming to the POSIX standard as well.

    If your sh is actually a link to bash, then this is what's causing that.

    Running sh --version; sh -c ': <(echo a)' should give you enough info.

    0 讨论(0)
  • 2020-12-07 03:24

    You should note that process substitution (<(...)) isn't specified by POSIX. So if you were running bash in POSIX mode by invoking it with sh or saying:

    set -o posix
    

    then you'd observe errors!

    From the bash manual:

    Starting Bash with the --posix command-line option or executing ‘set -o posix’ while Bash is running will cause Bash to conform more closely to the POSIX standard by changing the behavior to match that specified by POSIX in areas where the Bash default differs.

    ...

    Process substitution is not available.

    0 讨论(0)
提交回复
热议问题