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
Ooker
  • 1,969
  • 4
  • 28
  • 58
  • 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. – Charles Duffy Mar 13 '14 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... – twalberg Mar 13 '14 at 19:55

2 Answers2

25

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
Thomas Orozco
  • 53,284
  • 11
  • 113
  • 116
  • 2
    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. – Charles Duffy Mar 13 '14 at 18:36
  • @CharlesDuffy Added examples for those. I would however suspect apt will use `stdin` in most cases, but `DEBIAN_FRONTEND` could definitely break using `echo`. – Thomas Orozco Mar 13 '14 at 18:42
  • 4
    While `echo -ne '\n'` is a faithful copy from the problem description, the idiomatic way to write that is just `echo`. – tripleee Mar 13 '14 at 18:51
  • 1
    I would post the `-y` solution as the main answer, and the `echo` fix as a tangential comment. – tripleee Mar 13 '14 at 18:52
1
'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

Tanjin Alam
  • 1,728
  • 13
  • 15