Lehtia
Lehtia

Reputation: 169

Shell read from files with tail

I am currently trying to read from files with shell. However, I met one sytax issue. My code is below:

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

However, it returns me error syntax error near unexpected token('`

I tried with following How to use while read line with tail -n but still cannot see the error.

The tail command works properly.

Any help will be apprepricated.

Upvotes: 0

Views: 2353

Answers (2)

hek2mgl
hek2mgl

Reputation: 157947

process substitution isn't support by the posix shell /bin/sh. It is a feature specific to bash (and other non posix shells). Are you running this in /bin/bash?

Anyhow, the process substitution isn't needed here, you could simple use a pipe, like this:

tail -n +2 /pathToTheFile | cut -f5,6,7,8 | sort -u | while read -r line ; do
    echo "${line}"
done

Upvotes: 2

David C. Rankin
David C. Rankin

Reputation: 84541

Your interpreter must be #!/bin/bash not #!/bin/sh and/or you must run the script with bash scriptname instead of sh scriptname.

Why?

POSIX shell doesn't provide process-substitution. Process substitution (e.g. < <(...)) is a bashism and not available in POSIX shell. So the error:

syntax error near unexpected token('

Is telling you that once the script gets to your done statement and attempts to find the file being redirected to the loop it finds '(' and chokes. (that also tells us you are invoking your script with POSIX shell instead of bash -- and now you know why)

Upvotes: 1

Related Questions