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'
}









share|improve this question
























  • 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

















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'
}









share|improve this question
























  • 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















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'
}









share|improve this question















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






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 7 at 15:24

























asked Nov 7 at 15:03









grabantot

952617




952617












  • 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




















  • 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


















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



















active

oldest

votes











Your Answer






StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














 

draft saved


draft discarded


















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






























active

oldest

votes













active

oldest

votes









active

oldest

votes






active

oldest

votes
















 

draft saved


draft discarded



















































 


draft saved


draft discarded














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





















































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







這個網誌中的熱門文章

Xamarin.form Move up view when keyboard appear

Post-Redirect-Get with Spring WebFlux and Thymeleaf

Anylogic : not able to use stopDelay()