My bash script looks like:
read -p "Do you wish to continue?" yn
# further actions ...
And I just want to interact with this script using nodejs / child_process. How can I detect that it's waiting for the user input?
var spawn = require('child_process').spawn;
var proc = spawn('./script.sh');
proc.stdout.on("data", function(data) {
console.log("Data from bash");
}
proc.stdin.on("data", function(data) {
console.log("Data from bash"); // doesn't work :/
}
Thank you!
Have you try to use "expect" http://en.wikipedia.org/wiki/Expect ? It's a tool that "expect" some text (it use regular expression) and the bash script can automatically answers.
From the bash man page:
read -p promptDisplay prompt on standard error, without a trailing newline, before attempting to read any input. The prompt is displayed only if input is coming from a terminal.
And I don't think there a any way in node.js to detect that the script is waiting for input. The problems is actually that bash detects a non-terminal and disables the output to standard error. And even then you would have to read from stderr and not stdin to detect any waiting states.
In the end, as Antoine pointed out, you might have to use tools like empty or Expect to wrap your shell-scripts and trick Bash to think it is in a terminal.
Btw.: proc.stdin.write("yes\n") works fine. Thus you could work with the script, but won't get any prompts on proc.stderr and will not know when the script actually reads the input. You can also immediately proc.stdin.write the input even if the script is not yet at the read -p statement. The input is buffered until the scripts eats it up.