Read a question from stdout and write response to stdin
up vote
0
down vote
favorite
Consider this bash script:
# script-with-input.sh
# can't change this file :(
echo Hi
read -p "what's your name: " name
echo "That's a nice name."
read -p "what's you job: " job
echo Hello, $job $name!
How do I communicate with it using child_process.spawn
and give meaningful responses to the questions?
I need to get a question before giving a response (can't base my responses just on order), but my app just hangs awaiting for input and there is no question on stdout
.
I know this is related to stdout
being line buffered but don't know how to solve it.
const child_process = require('child_process')
const cmd = 'sh script-with-input.sh'
const child = child_process.spawn(cmd, , {shell: true})
child.stdout.on('data', data => {
console.log('data:', data.toString())
})
//child.stdin.write('Johnnmetalworkern')
function getResponse(question) { //...how do I get the question?
return question.includes('name') ? 'John' : 'metalworker'
}
node.js stdout stdin child-process spawn
add a comment |
up vote
0
down vote
favorite
Consider this bash script:
# script-with-input.sh
# can't change this file :(
echo Hi
read -p "what's your name: " name
echo "That's a nice name."
read -p "what's you job: " job
echo Hello, $job $name!
How do I communicate with it using child_process.spawn
and give meaningful responses to the questions?
I need to get a question before giving a response (can't base my responses just on order), but my app just hangs awaiting for input and there is no question on stdout
.
I know this is related to stdout
being line buffered but don't know how to solve it.
const child_process = require('child_process')
const cmd = 'sh script-with-input.sh'
const child = child_process.spawn(cmd, , {shell: true})
child.stdout.on('data', data => {
console.log('data:', data.toString())
})
//child.stdin.write('Johnnmetalworkern')
function getResponse(question) { //...how do I get the question?
return question.includes('name') ? 'John' : 'metalworker'
}
node.js stdout stdin child-process spawn
Sounds like you want to use thereadline
module from the standard library.
– Sven
Nov 7 at 15:28
@Sven Don't see how I can use it. It would've been useful as a replacement for the bash script. But there is no problem with the bash script, it does the job of asking questions just fine. I can't edit it anyway. The problem is that I am unable to read the questions properly.
– grabantot
Nov 7 at 15:47
similar question: stackoverflow.com/questions/15339379/…
– grabantot
Nov 7 at 16:52
one solution I found ispty.js
npm module, still looking for a better alternative...
– grabantot
Nov 7 at 17:54
add a comment |
up vote
0
down vote
favorite
up vote
0
down vote
favorite
Consider this bash script:
# script-with-input.sh
# can't change this file :(
echo Hi
read -p "what's your name: " name
echo "That's a nice name."
read -p "what's you job: " job
echo Hello, $job $name!
How do I communicate with it using child_process.spawn
and give meaningful responses to the questions?
I need to get a question before giving a response (can't base my responses just on order), but my app just hangs awaiting for input and there is no question on stdout
.
I know this is related to stdout
being line buffered but don't know how to solve it.
const child_process = require('child_process')
const cmd = 'sh script-with-input.sh'
const child = child_process.spawn(cmd, , {shell: true})
child.stdout.on('data', data => {
console.log('data:', data.toString())
})
//child.stdin.write('Johnnmetalworkern')
function getResponse(question) { //...how do I get the question?
return question.includes('name') ? 'John' : 'metalworker'
}
node.js stdout stdin child-process spawn
Consider this bash script:
# script-with-input.sh
# can't change this file :(
echo Hi
read -p "what's your name: " name
echo "That's a nice name."
read -p "what's you job: " job
echo Hello, $job $name!
How do I communicate with it using child_process.spawn
and give meaningful responses to the questions?
I need to get a question before giving a response (can't base my responses just on order), but my app just hangs awaiting for input and there is no question on stdout
.
I know this is related to stdout
being line buffered but don't know how to solve it.
const child_process = require('child_process')
const cmd = 'sh script-with-input.sh'
const child = child_process.spawn(cmd, , {shell: true})
child.stdout.on('data', data => {
console.log('data:', data.toString())
})
//child.stdin.write('Johnnmetalworkern')
function getResponse(question) { //...how do I get the question?
return question.includes('name') ? 'John' : 'metalworker'
}
node.js stdout stdin child-process spawn
node.js stdout stdin child-process spawn
edited Nov 7 at 15:24
asked Nov 7 at 15:03
grabantot
952617
952617
Sounds like you want to use thereadline
module from the standard library.
– Sven
Nov 7 at 15:28
@Sven Don't see how I can use it. It would've been useful as a replacement for the bash script. But there is no problem with the bash script, it does the job of asking questions just fine. I can't edit it anyway. The problem is that I am unable to read the questions properly.
– grabantot
Nov 7 at 15:47
similar question: stackoverflow.com/questions/15339379/…
– grabantot
Nov 7 at 16:52
one solution I found ispty.js
npm module, still looking for a better alternative...
– grabantot
Nov 7 at 17:54
add a comment |
Sounds like you want to use thereadline
module from the standard library.
– Sven
Nov 7 at 15:28
@Sven Don't see how I can use it. It would've been useful as a replacement for the bash script. But there is no problem with the bash script, it does the job of asking questions just fine. I can't edit it anyway. The problem is that I am unable to read the questions properly.
– grabantot
Nov 7 at 15:47
similar question: stackoverflow.com/questions/15339379/…
– grabantot
Nov 7 at 16:52
one solution I found ispty.js
npm module, still looking for a better alternative...
– grabantot
Nov 7 at 17:54
Sounds like you want to use the
readline
module from the standard library.– Sven
Nov 7 at 15:28
Sounds like you want to use the
readline
module from the standard library.– Sven
Nov 7 at 15:28
@Sven Don't see how I can use it. It would've been useful as a replacement for the bash script. But there is no problem with the bash script, it does the job of asking questions just fine. I can't edit it anyway. The problem is that I am unable to read the questions properly.
– grabantot
Nov 7 at 15:47
@Sven Don't see how I can use it. It would've been useful as a replacement for the bash script. But there is no problem with the bash script, it does the job of asking questions just fine. I can't edit it anyway. The problem is that I am unable to read the questions properly.
– grabantot
Nov 7 at 15:47
similar question: stackoverflow.com/questions/15339379/…
– grabantot
Nov 7 at 16:52
similar question: stackoverflow.com/questions/15339379/…
– grabantot
Nov 7 at 16:52
one solution I found is
pty.js
npm module, still looking for a better alternative...– grabantot
Nov 7 at 17:54
one solution I found is
pty.js
npm module, still looking for a better alternative...– grabantot
Nov 7 at 17:54
add a comment |
active
oldest
votes
active
oldest
votes
active
oldest
votes
active
oldest
votes
active
oldest
votes
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53192105%2fread-a-question-from-stdout-and-write-response-to-stdin%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Sounds like you want to use the
readline
module from the standard library.– Sven
Nov 7 at 15:28
@Sven Don't see how I can use it. It would've been useful as a replacement for the bash script. But there is no problem with the bash script, it does the job of asking questions just fine. I can't edit it anyway. The problem is that I am unable to read the questions properly.
– grabantot
Nov 7 at 15:47
similar question: stackoverflow.com/questions/15339379/…
– grabantot
Nov 7 at 16:52
one solution I found is
pty.js
npm module, still looking for a better alternative...– grabantot
Nov 7 at 17:54