shell sh:1:语法错误:"("意外错误

tmb3ates  于 2023-01-26  发布在  Shell
关注(0)|答案(2)|浏览(110)

我正在执行一个脚本,看起来像这样。

diff <(grep eth0 /proc/net/dev) <(sleep 1; grep eth0 /proc/net/dev)

我总是得到sh: 1: Syntax error: "(" unexpected,我试着把它写成一个.sh文件,试着用不同的shebang行,我需要创建一个脚本来执行这个。

nbysray5

nbysray51#

<(command)是一个名为Process substitution的bashism
您必须使用bash运行脚本,例如do bash script.bash
或者,如果您想使用sh,您可以将结果写入一个文件,如下所示:

grep eth0 /proc/net/dev > file1 
sleep 1 ; grep eth0 /proc/net/dev > file2 
diff file1 file2
tpgth1q7

tpgth1q72#

我认为您正在运行这个脚本sh code.sh,这意味着您正在使用sh来运行脚本,但这段代码暗示它是为bash编写的。
在某些系统上,shbash是相同的,但在其他系统上则不同;并且,当作为sh调用时,Bash会关闭一些非POSIX特性,因此使用正确的shell和正确的调用非常重要。
使用bash code.sh或更好的方法,使脚本可执行(chmod a+x code.sh),然后直接运行它(./code.sh

相关问题