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!
From the bash man page:
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 onproc.stderrand will not know when the script actually reads the input. You can also immediatelyproc.stdin.writethe input even if the script is not yet at theread -pstatement. The input is buffered until the scripts eats it up.