15

[EDITED] It can be considered as an extension to [this question][1]. echo | command The above command can be used to supply one 'ENTER' character to the command's first input request.

How can i supply the next 'ENTER' character to the same command in its second input request.

Please comment if any other details are required.

Am giving the specific example which i want to implement. I need to run SSH-keyGen commmand in my shell script. It will ask for following inputs:

  1. Enter the target file name
  2. Enter the pass phrase
  3. Enter the pass phrase again

How can we pass these three inputs to the command?

I tried with,

echo -ne "\n \n"| ssh-keygen  //which is passing two new lines for the first input request only.

and echo -ne "\n"|(echo -ne "\n"|ssh-keygen)// but still no positive result

Note: Am avoiding the input file name request in the above two command, just to make the things simple

pruthvi
  • 205
  • 1
  • 2
  • 11

5 Answers5

24

For example, you can use either

echo -e "\n"

or

echo -en "\n\n"

The -e option tells echo to interpret escape characters. \n is the newline (enter) character. So the first prints a newline due to \n, and then another one since echo usually appends a newline.

The -n option tells echo to suppress adding an implicit newline. To get two newlines, you thus need to specify two \n.

Edit:

The problem here is that ssh-keygen is special. Due to security considerations, the passphrase is not read from standard input but directly from the terminal! To provide a passphrase (even an empty one) you need to use the -P option. Since you then only need one ENTER (for the file path prompt), this command should work:

echo | ssh-keygen -P ''

(note the two ' with no space in between: they are important!)

DarkDust
  • 90,870
  • 19
  • 190
  • 224
  • It is not working @DarkDust . It is taking two new lines for the first input request only. – pruthvi Jun 09 '15 at 08:14
  • First of all, you've stated in your edit that you've used `echo "\n \n"`: that cannot work, you're missing the `-e` option. Next, `ssh-keygen` is special. Due to security considerations, the passphrase is not read from stdin but directly from the terminal! To provide a passphrase (even an empty one) you need to use the `-P` option: `echo | ssh-keygen -P ''` (note the two `'` with not space in between: they are important!) – DarkDust Jun 09 '15 at 08:58
  • I tried with -ne option only, I missed it to mention here( I added it now). "ssh-keygen -P '' " had solved my issue. Thanks very much.So here, because of the command i was using, it became an issue and not because of the way i was giving input . Can you add the same thing in the answer, so that i can select it as the answer. – pruthvi Jun 09 '15 at 09:28
  • This ssh-keygen trick was exactly what I was looking for. Thanks! – alta Jun 30 '18 at 23:07
8

The general solution is to use the yes command:

yes '' | command

yes will repeatedly output the string specified on the command line with a newline appended. If you run it with an empty string as an argument, it'll output an endless string of newlines.

Kenster
  • 23,465
  • 21
  • 80
  • 106
3

How about a heredoc with 2 empty lines:

command <<END


END

or use printf

printf "\n\n" | command
glenn jackman
  • 238,783
  • 38
  • 220
  • 352
2

You should take a look at expect:

#!/usr/bin/expect
#set timeout 10
set clientName [lindex $argv 0];
set hostName [lindex $argv 1];
set passWord [lindex $argv 2];

spawn ssh "$hostName";
expect "Password:";
send "$passWord\r";
expect "$hostName";
talamaki
  • 5,324
  • 1
  • 27
  • 40
sbriet
  • 21
  • 2
1

Solutions like echo -e "\n\n\n\n\n" will simulate ENTER by echo empty line only, without shell prompt.

will see:

➜  ~ echo -e "\n\n\n\n\n"






➜  ~ 

But if you want to simulate ENTER with shell prompt, here is my solution(define a function named entern):

zsh:

entern () {
      if [ "$1" ]; then
        for i in $(seq $1); do echo "${(%%)PS1}"; done
      else
        for i in $(seq 10); do echo "${(%%)PS1}"; done
      fi
}

Bash 4.4+:

entern () {
      if [ "$1" ]; then
        for i in $(seq 1 $1); do echo "${PS1@P}"; done
      else
        for i in $(seq 1 10); do echo "${PS1@P}"; done
      fi
}

see: How to print current bash prompt?

Use:

➜  ~ entern 5

will see:

➜  ~ entern 5 
➜  ~ 
➜  ~ 
➜  ~ 
➜  ~ 
➜  ~ 
➜  ~ 

default 10 times:

➜  ~ entern 
➜  ~ 
➜  ~ 
➜  ~ 
➜  ~ 
➜  ~ 
➜  ~ 
➜  ~ 
➜  ~ 
➜  ~ 
➜  ~ 
➜  ~ 
vikyd
  • 1,807
  • 1
  • 19
  • 28