I have a question. When use commands line to execute a bash file I can get it to work properly. It gets the input and export it to the environment variable.
How can I make it not hang and execute the block in prompt file?
prompt file
#!/bin/bash
echo "Enter your "
read input
echo "You said: $input"
Node.js file:
this is my node file that calls prompt file
checkIntegration(result)
.then(result => {
console.log(result, '123');
shell.exec('. prompt')
})
})
When I run it in my shell, I can enter information which is then printed:
$ . prompt
Enter your
Hello
You said: Hello
However, when I run it from node, I see the prompt but it won't accept any input:
How can I make my program read user input from node's terminal?
my folder structure
checker.js
const {exec} = require('child_process');
var shell = require('shelljs');
function getPrompt() {
shell.exec('. prompt');
}
getPrompt()
tokens.txt
GITLAB_URL
GITLAB_TOKEN
GITLAB_CHANNEL
GITLAB_SHOW_COMMITS_LIST
GITLAB_DEBUG
GITLAB_BRANCHES
GITLAB_SHOW_MERGE_DESCRIPTION
SLACK_TOKEN
prompt
#!/bin/bash
SOURCE="$PWD"
SETTINGS_FILE="$SOURCE/tokens.txt"
SETTINGS=`cat "$SETTINGS_FILE"`
for i in ${SETTINGS[@]}
do
echo "Enter your " $i
read input
if [[ ! -z "$input" ]]; then
export "$i"="$input"
fi
done


$inputin this case?nodeto see the problem.process.stdinis defaulting topipe, so your child process is hanging forever waiting for Node to write something to that pipeline; see nodejs.org/api/child_process.html#child_process_options_stdio. promptcan not and will not change the environment that your Node process has, or the environment used by future scripts started by the same process.exports will be completely undone as soon as the shell invoked with. promptexits (because, like all other environment variable changes, they are local to the process where the change is made and its children). Using.orsourceworks when you're trying to make a change to the shell from which you invoked that command, but here, you're letting that shell exit as soon as it's done sourcing the script, utterly mooting the point.