Linux 命令在 shell 上运行良好但在脚本中运行不正常

标签 linux bash shell

<分区>

我有2 Linux 中的文件如下图

file1

test_1
test_3
test_5
test_6

file2

test_1,smoke_test
test_2,rain_test
test_3,sun_test
test_4,wind_test

我想比较这两个文件并删除file1中的表存在于 file2 中那是comma(,)之前的第一部分

需要的输出:

file3

test_5
test_6

我试过如下

grep -v -Ff <(cut -d',' -f1 file2) file1 >file3

我得到了我想要的。

现在当我写一个脚本时它抛出一个错误

new.sh: line 67: syntax error near unexpected token `('

Script

#!/bin/bash

grep -v -Ff <(cut -d',' -f1 file2) file1 >file3

我正在运行它:

sh -x script.sh

最佳答案

您正在使用 sh 运行脚本而不是 bash ,所以你没有得到 bash<(...) 这样的扩展名过程替代。运行它:

bash -x script.sh

或者只是:

./script.sh

后者将使用 #! 中指定的解释器行。

关于Linux 命令在 shell 上运行良好但在脚本中运行不正常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48156651/

相关文章:

linux - Angular 变化检测不起作用

C++:检测USB加密狗的守护程序

arrays - BASH - 作为数组名称一部分的变量(第 2 部分)

linux - 为什么我的 awk 命令多打印一行?

android - 检测插入的 USB 设备

linux - 使用依赖于另一个库的库的 CMake 项目

linux - Bash:如何截断数组?

linux - Bash - 重命名其中有“的文件

java - 为什么从 JSch 中的 shell channel 输出读取永远不会以 -1 结尾?

linux shell 在空参数上测试 `-d`