I have this:


node x.js | delete_lock $0 < ${my_named_pipe} &

I am looking to read from my_named_pipe into the stdin of the node.js process, and upon certain input, then write to stdout in the node.js process which would then be captured by delete_lock.

My main question is, how can I reference stdin in delete_lock in bash? I don't think it's $0, how can I do it?

Is this the only way to do it?

node x.js | { while read line; delete_lock $line; } < ${my_named_pipe} &

You say you need the Node application to read from the pipe, then let's do that:

node x.js <"$my_named_pipe"

Then you say you want the function to read the output from that, then let's do that too:

node x.js <"$my_named_pipe" | delete_lock

Note that | is a command terminator, which means that in

command1 | command2 <thing

it's command2 that reads from thing.

Responding to comments:

The delete_lock function can read its standard input, which comes from the Node application. If it, for example, has to do some action depending on a pattern in the output from the Node application, it may want to use grep:

delete_lock () {
    if grep -q 'PATTERN'; then
        # some command here

If you need the Node application to keep going after the action has been performed by the function, then you need to continue consuming its output:

delete_lock () {
    if grep -q 'PATTERN'; then
        # some command here
    cat >/dev/null

Otherwise, the Node application will receive a SIGPIPE signal and terminate.

  • thanks, do you have thoughts on alternative to using read to get a var representing stdin? – Alexander Mills Apr 20 '18 at 8:52
  • @AlexanderMills I don't really see a use for that. delete_lock can parse the output of the Node application in any way it wants. If it wants to act on certain input, then use grep -q (for example) on the input stream. Basically, I can't say much about this because you have not described what delete_lock needs to be doing. – Kusalananda Apr 20 '18 at 8:58
  • yeah I don't follow - maybe can you add an example of delete_lock reading from the node command? I am not so good at bash that I know how, I can only guess that the read command would then go inside the delete_lock function. – Alexander Mills Apr 20 '18 at 9:01
  • @AlexanderMills See updated answer. – Kusalananda Apr 20 '18 at 9:10
  • yeah see I don't understand how to read the input - how I can reference the stdin - say "dog" is sent to stdin, how do I reference it? (in the delete_lock function). – Alexander Mills Apr 20 '18 at 20:26

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.