11
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

Billy Chan
  • 141
  • 1
  • 1
  • 9
  • 5
    do you have `#!/bin/bash` as the first line of your script? Else edit you Q to include what is there. Good luck. – shellter Mar 22 '16 at 01:21
  • remove space between < – nobody Mar 22 '16 at 01:21
  • 5
    @inquisitive_mind: No, it is a valid syntax for `bash` (but as @shellter says, not valid for `sh`). `< <( ... )` is different from `<<( ... )` (in fact, the *latter* is incorrect). – Amadan Mar 22 '16 at 01:28
  • @shellter Yes I have. Furthermore, it works perfectly if I copy the code and paste in the terminal directly. it just doesnt work on sh. – Billy Chan Mar 22 '16 at 03:42
  • "doesn't work on `sh`" is different than "doesn't work with `#!/bin/bash`" as the first line of your program. If Windows was involved in script, then `dos2unix myScript`. Also did you `chmod 755 myScript` ? (I'm out of ideas). Good luck. – shellter Mar 22 '16 at 04:11
  • @shellter I have tried both sh and bash , even csh , nth of this work – Billy Chan Mar 22 '16 at 05:41
  • I am out of idea too as I can copy and paste in the terminal and run perfectly but not in type of sh / bash / csh script file – Billy Chan Mar 22 '16 at 05:42

2 Answers2

25

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

Lin
  • 620
  • 7
  • 11
  • 1
    I give up using sh or bash. I directly use ./foo.sh then – Billy Chan Mar 22 '16 at 05:57
  • @BillyChan : Yes, no shell programmer uses `sh foo.sh`, we rely on the "she-bang" line to keep all needed "code" in one file. I'm glad that Lin was able to guess what you were doing. Good luck to all! P.S. This comes up ofetn enough on S.O. .. where did you get the idea to use `sh foo.sh`? It should be removed as "only a last resort" techinque (IHMO). – shellter Mar 22 '16 at 12:00
  • There's nothing wrong with running `sh` et al. directly; it's just more typing than making the script executable and specify the shell with the shebang. (Of course, if the script is intended to be run by another program that expects an executable, then yes, it's necessary to make it executable.) – chepner Mar 22 '16 at 12:22
7

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.

ganzpopp
  • 364
  • 3
  • 12