Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

syntax error near unexpected token `<'

StudentAnwser=()
inputScriptFile=001.sh

while IFS= read -r line;
do
    StudentAnwser+=( "$line" )
done < <( sh $inputScriptFile test.txt )

it returns a error

foo.sh: line 22: syntax error near unexpected token `<'
foo.sh: line 22: `  done < <( sh $inputScriptFile test.txt )'

what's wrong with that? I follow the solution from other question for reading line from result

like image 847
Billy Chan Avatar asked Mar 22 '16 01:03

Billy Chan


People also ask

How do I fix this error syntax error near unexpected token?

If you execute the code written in the Windows OS in the Cygwin, you may get the Syntax error near unexpected token '('. To fix the error, you need to clear the carriage return characters using the DOS to Unix command line tool as a text file format converter.

What Is syntax error near unexpected token `('?

The error message syntax error near unexpected token `(' occurs in a Unix-type environment, Cygwin, and in the command-line interface in Windows. This error will most probably be triggered when you try to run a shell script which was edited or created in older DOS/Windows or Mac systems.

How do I fix an unexpected token?

As you write your JavaScript application, the unexpected token error always occurs because JavaScript expected a specific syntax that's not fulfilled by your current code. You can generally fix the error by removing or adding a specific JavaScript language symbol to your code.

What does syntax error mean in Linux?

What Does Syntax Error Mean? A syntax error in computer science is an error in the syntax of a coding or programming language, entered by a programmer. Syntax errors are caught by a software program called a compiler, and the programmer must fix them before the program is compiled and then run.


2 Answers

You get the error because process substitution (the <(some command) part) is not a standard feature (defined in POSIX) in sh, which means it may work on some OS but may not in others or in the same OS with different configuration.

You clarified that you have #!/bin/bash at the top of your script, but I guess you still run the script via sh foo.sh, as such, #!/bin/bash will be ignored and the script is interpreted by sh.

I assume your default shell is bash (run echo $SHELL), so all problems are gone if you paste the script in terminal and execute.

==== UPDATE ====

Possible solution if my assumption is correct:

Leave #!/bin/bash as it is, make your script an executable by chmod +x foo.sh. Then run it directly by ./foo.sh

like image 138
Lin Avatar answered Oct 16 '22 07:10

Lin


Found another solution: process substitution is not a POSIX-compliant feature and, depending on your system and shell, it might need to be enabled.

Use the following line in your script, before the loop:

set +o posix

to enable it. It might be that your system doesn't support it at all, then this is of no help.

like image 41
ganzpopp Avatar answered Oct 16 '22 08:10

ganzpopp