My question is similar to this one: How to detect if my shell script is running through a pipe?. The difference is that the shell script I’m working on is written in Node.js.
Let’s say I enter:
echo "foo bar" | ./test.js
Then how can I get the value "foo bar"
in test.js
?
I’ve read Unix and Node: Pipes and Streams but that only seems to offer an asynchronous solution (unless I’m mistaken). I’m looking for a synchronous solution. Also, with this technique, it doesn’t seem very straightforward to detect if the script is being piped or not.
TL;DR My question is two-fold:
- How to detect if a Node.js script is running through a shell pipe, e.g.
echo "foo bar" | ./test.js
? - If so, how to read out the piped value in Node.js?
./test.js $(echo 'foo bar')
– Fibrovascular