问题
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