9

I know that this question is answered many times, but I still can't figure out how to do it. Maybe it's because I don't know the correct keyword to search for.

Using

echo -ne '\n' | enter

doesn't work. My code is:

#! /bin/bash
#Grub-customizer
sudo add-apt-repository ppa:danielrichter2007/grub-customizer
echo -ne '\n' | return
sudo apt-get update
sudo apt-get install grub-customizer
2
  • Don't try. When you do echo | some_command, you can get either the exit status of echo or the exit of some_command, depending on how your shell is currently configured. You get much, much more consistent and reliable results by just invoking some_command in such a way that it doesn't read from stdin at all. Commented Mar 13, 2014 at 18:37
  • What is this enter command of which you speak? Also, return pretty much ignores it's standard input, so piping something into it doesn't accomplish much... Commented Mar 13, 2014 at 19:55

2 Answers 2

27

You're supposed to pipe the \n into the command that's going to be receiving it (otherwise it won't ever see it!):

echo -ne '\n' | sudo add-apt-repository ppa:danielrichter2007/grub-customizer
echo -ne '\n' | sudo apt-get install grub-customizer

Now, the right solution here would instead be to use the -y flags instead:

sudo add-apt-repository -y ppa:danielrichter2007/grub-customizer
sudo apt-get install -y grub-customizer
Sign up to request clarification or add additional context in comments.

4 Comments

The pipe may or may not work -- do you know that apt-get and apt-add-repository read from stdin and not /dev/tty? Might be better to more strongly reinforce the -y flag advice, which is a much more sure thing.
@CharlesDuffy Added examples for those. I would however suspect apt will use stdin in most cases, but DEBIAN_FRONTEND could definitely break using echo.
While echo -ne '\n' is a faithful copy from the problem description, the idiomatic way to write that is just echo.
I would post the -y solution as the main answer, and the echo fix as a tangential comment.
0
'echo -ne "\n" | ./create.sh \n', // this command is for pressing enter each time the shell script is asking user to press enter
'echo -ne "\n" | yourcommand \n', //tempate 

  const client = new Client();
  const cmds = [
    'ls -lah \n', // \n is important 
    'cd /mnt \n',
    'echo -ne "\n" | ./create.sh \n', // this command is for pressing enter each time the shell script is asking user to press enter
    'pwd \n',
    'ls -lah \n',
    'exit \n',
  ];
  client.on('ready', () => {
      console.log('Client :: ready');
      client.shell((err, stream) => {
        stream.on('close', (code) => {
          console.log('stream :: close\n', { code });
        }).on('data', (myData) => {
          console.log('stream :: data\n', myData.toString());
        }).on('exit', (code) => {
          console.log('stream :: exit\n', { code });
          client.end();
        }).on('error', (e) => {
          console.log('stream :: error\n', { e });
          rej(e);
        });
        for (let i = 0; i < cmds.length; i += 1) {
          const cmd = cmds[i];
          stream.write(`${cmd}`);
        }
      });
    }).connect({
    host: '127.0.0.1',
    port: 22,
    username: 'root',
    password: 'root',
  });

Reference Click HERE

Comments

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.