0

我目前正在尝试使用 shell 从文件中读取。但是,我遇到了一个语法问题。我的代码如下:

while read -r line;do
    echo $line
done < <(tail -n +2 /pathToTheFile | cut -f5,6,7,8 | sort | uniq )

但是,它返回我错误syntax error near unexpected token('`

我尝试使用 tail -n遵循How to use while read line但仍然看不到错误。

tail 命令正常工作。

任何帮助都将得到批准。

4

2 回答 2

2

posix shell /bin/sh 不支持进程替换。它是 bash(和其他非 posix shell)特有的功能。你在 /bin/bash 中运行它吗?

无论如何,这里不需要进程替换,您可以简单地使用管道,如下所示:

tail -n +2 /pathToTheFile | cut -f5,6,7,8 | sort -u | while read -r line ; do
    echo "${line}"
done
于 2020-03-10T03:19:13.257 回答
1

您的解释器必须#!/bin/bash不是和/或您必须使用而不是#!/bin/sh运行脚本。bash scriptnamesh scriptname

为什么?

POSIX shell 不提供process-substitution。进程替换(例如< <(...))是一种 bashism,在 POSIX shell 中不可用。所以错误:

syntax error near unexpected token('

告诉您,一旦脚本到达您的done语句并尝试找到被重定向到它找到'('并阻塞的循环的文件。(这也告诉我们您正在使用 POSIX shell 而不是 bash 调用您的脚本——现在您知道为什么了)

于 2020-03-10T03:15:09.193 回答