Luca Davanzo
Luca Davanzo

Reputation: 21510

Launch node script from bash script

I've my program in bash and I want to launch a node program to get the string that it return, like in this way:

#!/bin/bash

mystring=$( node getString.js)
mplayer $mystring

Googling I found that I should inlcude

#!/usr/bin/env node

But I need to use string to give it to mplayer.. any ideas?

Solution

As Zac suggesting (and thanks to this link) I solved my problem in this way:

script.sh

#!/bin/bash 

mplayer ${1}

script.js

/* do whatever you need */
var output="string"
var sys = require('sys');
var exec = require('child_process').exec;
function puts(error, stdout, stderr) { sys.puts(stdout);  }
exec("./script.sh " + output, puts);

Upvotes: 2

Views: 4266

Answers (1)

Zac B
Zac B

Reputation: 4232

Consider simply writing an executable Node script (with the #!/bin/env node line), and, instead of using Bash, just use Node to run the external UNIX command. You can use the child_process module for this, as illustrated in this example. This question is also helpful when debugging shell-style subcommands in Node scripts.

If your example really is all you need to do in Bash, this should be sufficient. The #!/bin/env node line allows your script, once marked as executable, to run as its own program, without having to be invoked with node.

Upvotes: 1

Related Questions