Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

I have this NodeJS script:

var util  = require('util'),
process = require('child_process'),
ls    = process.exec('test.sh');

ls.stdout.on('data', function (data) {
   console.log(data.toString());
   ls.stdin.write('Test');
});

and this shell script:

#!/bin/bash
echo "Please input your name:";
read name;                 
echo "Your name is $name";

I tried to run the NodeJS script and it is stuck at "Please input your name:". Does anyone know how to pass input from my NodeJS script to my shell script ?

Thanks

share|improve this question
add comment

2 Answers

up vote 2 down vote accepted

Did you try adding '\n' to the end of your input (e.g. ls.stdin.write('Test\n');) to simulate pressing return/enter?

Also, you want process.spawn, not process.exec. The latter does not have a streaming interface like you are using, but it instead executes the command and buffers stdout and stderr output (passing it to the callback given to process.exec()).

share|improve this answer
    
thanks, it solved the problem –  Rudy Lee Jun 25 at 23:30
add comment

You will have to say something like this:-

ls.stdin.write('test\n');

OR

you can inherit stdanderd streams if you want input from user using spawn.

like this:-

var spawn = require('child_process').spawn;
spawn('sh',['test.sh'], { stdio: 'inherit' }); 
share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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