0

Consider this scenario:

diff file.txt <( cat file.txt | grep -v '^\s*#'| sed '/^$/d' )

this is an example of redirect from right to left, in which you process a file and redirect the result as input to diff. Similar use case has been proposed here, which also works fine.

However, attempt to redirect result of file processing to a code block in a similar fashion fails:

  while read I; do
    …
  done <( cat $FIL | <do_something_here> )

Syntax error: "(" unexpected

Or, if you try this:

done < <( cat $FIL | <do_something_here> )

Syntax error: redirection unexpected

How do you redirect output of some process to code block as input?

1 Answers1

3

<(...) process substitution is a feature of the Korn shell. It is also available in zsh and bash. rc and derivatives and fish also have process substitution support but with a different syntax, while in the yash shell <(...) is syntax for a different feature: process redirection.

The wording of that Syntax error: redirection unexpected error suggests your shell is an Almquist shell derivative such as dash. AFAIK, neither process substitution nor process redirection have been added to any ash derivative.

while ...; done <(cmd) would work in yash, while ...; done < <(cmd) would work in AT&T ksh, zsh and bash, but neither are standard sh syntax and neither would work in dash.

In dash (or any sh implementation), you'd need cmd | while ...; done, though note that in dash, that while loop would run in a subshell.

You could do:

while ...; done << EOF
$(cmd)
EOF

Though beware it stores the whole output of cmd (stripped of all NUL characters (in dash at least) and all trailing newline characters) in memory (and adds back one extra newline character).

In any case, using a while read loop is often an indication you're taking the wrong approach, especially if it's just for text processing.