3

I am running a Node.js script in Linux that prompts (via lib) user for a code. I get the process id of the script:

pgrep -f test.js 

and then pass it the code with a new line to simulate Enter key:

echo -e "1234\n" > /proc/88888/fd/0

The code 1234 gets passed, a new line is added as well, but it didn't trigger Enter key and the script does not proceed. However, when I manually press Enter key in shell, the script does recognise the Enter key. So the question is how can I reliably send Enter key to another process/script?

Following is the code of test.js script:

inquirer = require('inquirer');

async function plztest() {

let { code } = await inquirer.prompt([
    {
        type: 'input',
        name: 'code',
        message: 'Enter code',
    },
]);
console.log(code);
process.exit();
};
      
plztest();
2
  • could you share the file you are feeding data to? so that any possible solution can be tested ? Commented Nov 16, 2020 at 6:19
  • added test.js code Commented Nov 16, 2020 at 8:16

1 Answer 1

1

Thank you for providing the code snippet of the javascript file, I've got a solution for the same.

The problem

is that you are a bit confused about the usage of pipes and redirections.

  • Pipe | is used to pass output to another program or utility.
  • Redirect > is used to pass output to either a file or stream.

A more detailed answer about pipes and redirections is given here

The Solution

Since now we know that we need to use pipes here, something like this would solve the problem.

echo -e "1234" | /proc/88888/fd/0

Tests

I ran the script you have provided in the question, and the following are the output screenshots. Local Screenshots of script

Sign up to request clarification or add additional context in comments.

2 Comments

echo "123" | node app.js certainly works. But my use case requires echo -e "1234" | /proc/88888/fd/0 and doing so gives me Permission Denied error.
However, echo -e "1234" | tee /proc/88888/fd/0 worked, but still the script didn't recognise Enter command and thus does not proceed.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.