sh -c and process substitution [duplicate]

纵饮孤独 提交于 2019-12-23 17:53:15

问题


In terminal (bash) the following works fine:

cat <(echo "hello") 

But if I do:

sh -c 'cat <(echo "hello")'

I get

sh: 1: Syntax error: "(" unexpected

Can you explain the reason why?

Btw, my overall aim is to write this command in a shell script:

watch -n 1 'cat <(iptables -L INPUT) <(iptables -L FORWARD)'

but it won't work, the reason seems to be the above problem.


回答1:


sh is often dash not bash (see man sh). dash doesn't do process substitution, only POSIX stuff.

You'll need to do:

  bash -c 'cat <(echo "hello")'

ksh & zsh can do process substitution too.

With your example, you can simply do:

watch -n 1  'iptables -L INPUT;  iptables -L FORWARD'

no need for an advanced shell or process subtitution.




回答2:


The reason for the syntax error is that sh is not linked to bash on your system and does not understand process substitution, which is not part of the POSIX standard.

I would recommend the following command which is much cleaner and works with any POSIX compatible shell:

while true ; do
    clear
    iptables -L INPUT
    iptables -L FORWARD
    sleep 1
done


来源:https://stackoverflow.com/questions/39069179/sh-c-and-process-substitution

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!