Need to call function written inside a shell script from node js - node.js

I need to call a function written inside a shell script from node js. I have tried the command "source test.sh a" , where 'a' is the method inside test.sh in shell command. It works but from nodejs how to call it using execFile/spawn.

With child_process spawn, you could do it with the { shell: true } option (to spawn a shell), but it seems that the exec method is more appropriate in this case:
const { exec } = require('child_process');
exec('source ./test.sh && a');

Related

Calling processing-java from nodejs on Windows 10

I am having some problems to execute the processing-java on windows from nodejs. When I use the following
processing-java --sketch=$pwd/sketch/ --run
in the console it works fine and the sketch is executed, window opens and the result is a file written to a directory where I need it.
But when I use the same as a string in the node file to execute it, it does not open a window and no file has been written, but the callback function processOutput is executed, hence the somehow the execution happened.
I added
const exec = require('child_process').exec;
to the js file which is used for executing child processes in node.
$pwd is the PowerShell equivalent to pwd on MacOS.
exec should execute the shell command and call the callback function. The callback function is executed, but not the sketch.
const cmd = 'processing-java --sketch=$pwd/sketch/ --run';
exec(cmd, processOutput);
What am I missing? Any help appreceated.
Many thanks, C.
child_process.exec() does not execute in a powershell but in a CMD terminal. Thus you can't use powershell variables like $pwd. You could try using %CD% instead
const cmd = 'processing-java --sketch=%CD%/sketch/ --run';
exec(cmd, processOutput);

Nodejs child_process spawn with $ variable

Question: Can I access $variable via {spawn} = require("child_process") ?
Background: I have already tried using exec("echo $HOME"); which output the correct value.
But when I tried to use spawn("echo",["$HOME"]), I got $HOME
And how to define the variable by using spawn? Currently I'm setting the child_process option {env:{data:1000}}, is this correct or not ?
by default, spawn does not create a shell and then execute the command within that shell (as appose to exec which starts a shell first and then executes the command within that shell).
Since there is no shell, the "$HOME" environment variable is not set. You can set spawn to execute the command within a shell:
spawn("echo",["$HOME", { shell: true });

How can i open an interactive bash shell from nodejs REPL console

I want to open a bash shell from nodejs console.
I have tried everything from child_process module, but i did not managed to find a solution where i can see $ prompt.
Thanks
You can make the child inherit the stdio, here you have a working example (Linux) :
const { spawn } = require('child_process')
const shell = spawn('sh',[], { stdio: 'inherit' })
shell.on('close',(code)=>{console.log('[shell] terminated :',code)})
example output :
sh-4.4$ uname
Linux
sh-4.4$ exit
exit
[shell] terminated : 0
Remember to replace sh with the correct shell for your platform requirements

Simple bash commands fails, cannot execute binary file

I try to execute some simple bash in node bu get an error:
/usr/bin/file: /usr/bin/file: cannot execute binary file
'use strict';
var spawn = require('child_process').spawn;
var process = spawn('bash', ['file']);
process.stdout.on('data', function(data){
console.log(data.toString());
});
process.stderr.on('data', function(data){
console.log(data.toString());
});
I even gave the script chmod+x and i run it with node script.js
Any ideas?
It looks like when you're spawning the child process, you're actually trying to run the "file" command, not a shell script, so bash is barking at you.
It would be equivalent to typing this on the command line: "bash file".
You'll want to write a shell script and pass that as the parameter to the bash process.
So, write a script called "do_something.sh" and then run your code with ['do_something.sh'] as the parameter to bash rather than ['file']:
var process = spawn('bash', ['do_something.sh']);

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);
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.

Resources