粘贴不是从脚本运行而是在命令行上运行

时间:2016-10-26 13:54:35

标签: bash paste

我正在运行这个班轮作为更大脚本的一部分 -

paste -d '' <(cut -c"1-5" file1) <(cut -c"1-2" file2) <(cut -c"8-" file1)

这将获取第一个文件file1的前5个字符,file2的字符1-2,然后是file1以后的字符8,并将它们全部粘贴在一起。

$cat file1
1234567890
1234567890
1234567890
1234567890
1234567890

$cat file2
abcdefghij
abcdefghij
abcdefghij
abcdefghij
abcdefghij

output-
12345ab890
12345ab890
12345ab890
12345ab890
12345ab890

这可以从命令行(上面显示的输出)中按预期完美地工作。

但是,如果我将该行放入脚本(如此处所示) -

$cat a.sh

#!/bin/bash
paste -d '' <(cut -c"1-5" a) <(cut -c"1-2" b) <(cut -c"8-" a)

如果我运行脚本,我会收到此错误 -

$sh a.sh
a.sh: line 2: syntax error near unexpected token `('
a.sh: line 2: `paste -d '' <(cut -c"1-5" a) <(cut -c"1-2" b) <(cut -c"8-" a)'

任何想法在这里出了什么问题?我通过shellcheck运行它,它告诉我脚本很好。

1 个答案:

答案 0 :(得分:1)

按以下方式运行;

./a.sh 

bash a.sh
bash和sh是两个不同的shell。 bash具有更多功能和更好的语法。