语法错误:“(”意外

Atu*_*tul 6 command-line bash scripts

直接在终端上执行以下命令时,其工作正常:

comm -2 -3 <(sort FileOne.txt) <(sort FileTwo.txt) > myFile.txt
Run Code Online (Sandbox Code Playgroud)

但是在尝试通过.sh文件执行它时,出现错误:Syntax error: "(" unexpected

这是我的代码:

#!/bin/bash
##this will return the unique lines from the first file.
comm -2 -3 <(sort FileOne.txt) <(sort FileTwo.txt) > myFile.txt
Run Code Online (Sandbox Code Playgroud)

我这样运行脚本:

sh ./myfilename.sh
Run Code Online (Sandbox Code Playgroud)

请帮我解决它。

谢谢!

Art*_*ild 14

如果需要,请首先运行以下命令使脚本可执行:

chmod +x myfilename.sh
Run Code Online (Sandbox Code Playgroud)

然后,像这样运行脚本:

./myfilename.sh
Run Code Online (Sandbox Code Playgroud)

或者使用 bash 显式运行它,如下所示:

bash myfilename.sh
Run Code Online (Sandbox Code Playgroud)

#!/bin/bash(前两个命令本质上是相同的,因为脚本开头有 bash shebang 。)

不要这样跑:

sh myfilename.sh
Run Code Online (Sandbox Code Playgroud)

因为这样您将使用 Dash ( ) 而不是使用 Bash 运行脚本sh(因为您的脚本具有 Bash 特定的语法)。