Frage

I have a script that runs in my zsh.

cat foo.txt <(node bar.js) > out.txt

This little script is configured in a node.js module which uses /bin/sh for execution. The problem is that sh fails at the <

/bin/sh: -c: line 0: syntax error near unexpected token `('

The goal is to concatenate contents of foo.txt and the output of an executed script into a single out.txt

Can I somehow achieve the same thing using sh?

War es hilfreich?

Lösung

You can achieve the same effect as the original script by grouping commands within {...} like this:

{ cat foo.txt; node bar.js; } > out.txt

Using <(...) just so that you can cat it is awkward and inefficient. Using grouping like this is better and more portable, so I think it's an improvement over the original script.

Andere Tipps

You can use the - stdin-marker when running cat, and redirect the output of your node command to cat using plain piping:

node bar.js | cat foo.txt - > out.txt

This is pretty standard. It should work in any shell.

Functionally equivalent to process substitution is the use of a named pipe (and depending on what the underlying system supports, is how bash implements process substitution).

# Create a named pipe - it's like a file, but of limited size
# A process that writes to it will block until another process reads some
# data and frees up some space for more output
mkfifo node_js_output

# Run node in the background, redirecting its output to the pipe
node bar.js > node_js_output

# Concatenate foo.txt and the output from node to out.txt
cat foo.txt node_js_output > out.txt

# When node bar.js completes, it will exit. Once cat has finished
# reading everything written to node_js_output, it will exit as well,
# leaving behind the "empty" node_js_output. You can delete it now
rm node_js_output
Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top