AKIRK
AKIRK

Reputation: 117

How run a bash script on nodejs output

I have a nodejs script that produce some output like that (it scrape website and output url) :

process.stdout.write(url)

And I want my bash to use this url (basically it just curl the url) and do some stuff.

I try node myapp | bash mybash ==> Error: write EPIPE

I try node myapp | grep 'www' ==> I see url spawn

I probably missing something about stdin stdout For the moment, in my bash I just echo $1

Upvotes: 0

Views: 2142

Answers (3)

cookie s
cookie s

Reputation: 75

I often use this and integrate it with my own tools. It might help you:

const { exec } = require('child_process');
exec('shell.sh value', (err, stdout, stderr) => {
  if (err) {
    console.error(err)
  } else {
   console.log(`stdout: ${stdout}`);
   console.log(`stderr: ${stderr}`);
  }
});

Learn more about child processes.

Upvotes: 0

AKIRK
AKIRK

Reputation: 117

The solution that fit for me was :

In nodejs file : process.stdout.write(url+' ')

And in bash file:

read url_from_node

url_list=$( echo $url_from_node | tr ' ' '\n') then process this list...

Then node myApp | mybash works

Upvotes: 0

tripleee
tripleee

Reputation: 189397

Your question is rather unclear, but I guess you are looking for

node myapp |
while IFS= read -r url; do
    : something with "$url"
done

If mybash expects a single command-line argument, maybe you want mybash "$url" inside the loop; though perhaps in your case you want to instead refactor your script to do the read loop instead, or as an option.

Very often you want to avoid this and write e.g. an Awk script or something instead; but this is how you read from standard input in Bash.

Upvotes: 1

Related Questions