-
Notifications
You must be signed in to change notification settings - Fork 30k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Weird behaviour when spawning bash scripts #29831
Comments
The bash man page describes why prompt output goes to stderr, and why it doesn't appear at all when stdin is redirected. I think you are needing a pty emulation library, pipes alone don't allow the kind of interaction with a shell you are attempting. Consider https://github.com/microsoft/node-pty |
Thanks for the quick reply. node-pty seems to do the trick. |
This was referenced Mar 11, 2021
This was referenced Apr 26, 2021
This was referenced Nov 4, 2022
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
There seem to be multiple issues when spawning bash scripts. Regular binaries (e.g. php, docker) seem to work fine at this point. I am running nodejs v10.16.3 on ubuntu.
Summary
When spawning a bash script with nodejs with stdin set to inherit, the output of the read command inside the bash script is not printed to stdout. It is printed to stderr although I think it doesn't belong there.
When setting stdin to pipe and piping process.stdin to this pipe, the output of the read command is not available anymore on stderr. Also, the app can only be exited by hitting enter after the child process exited.
Test cases
Consider the following bash script to be spawned via nodejs
Case 1.) nodejs file spawning the bash script with stdin set to inherit
Calling the script above in a terminal ("node index.js"), the output is the following
The weird part here is that the "read -p" text "Input: " is written to stderr. Is that a bug with bash or its "read" command? Or is nodejs doing something wrong. Everything else seems to work right at this point.
Case 2.) nodejs file spawning the bash script with stdin set to pipe
Calling the script above in a terminal ("node index.js"), the output is the following
You can see the first echo ("Greeting") of the bash script and it is written to stdout. The text of the "read" command ("Input: ") is not visible anymore. The rest of the script is running as expected except that after the child process exits I have to hit enter a second time to exit the whole application.
Case 3.) nodejs file spawning the bash script with stdout, stderr and stdin set to pipe
The output and behaviour is basically the same as in case 2.
The "Input: " line is not visible and I have to hit enter after "child process exited..." to exit the application.
Addendum
For one of our products I need to automate different external applications (e.g. php scripts, docker commands, bash scripts). I want to pipe stdin, stderr and stdout so that I can read from stdout and write to stdin. If a specific line is written to stdout (e.g. "Do you really want to start the process now?") I 'answer' by writing "y\n" to stdin and the external process continues.
Because of the problems described above I am not able to do this.
The text was updated successfully, but these errors were encountered: