[英]How to simulate two consecutive ENTER key presses for a command in a bash script?
[EDITED] It can be considered as an extension to [this question][1]. [已编辑] 它可以被视为对 [这个问题] [1] 的扩展。
echo | command
echo | command
The above command can be used to supply one 'ENTER' character to the command's first input request. echo | command
上述命令可用于向命令的第一个输入请求提供一个“ENTER”字符。
How can i supply the next 'ENTER' character to the same command in its second input request.我如何在第二个输入请求中为同一命令提供下一个“ENTER”字符。
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.我需要在我的 shell 脚本中运行
SSH-keyGen
。 It will ask for following inputs:它将要求以下输入:
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
和
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注意:我避免了上面两个命令中的输入文件名请求,只是为了使事情简单
For example, you can use either 例如,您可以使用其中之一
echo -e "\n"
or 要么
echo -en "\n\n"
The -e
option tells echo
to interpret escape characters. -e
选项告诉echo
解释转义字符。 \\n
is the newline (enter) character. \\n
是换行符(输入)字符。 So the first prints a newline due to \\n
, and then another one since echo
usually appends a newline. 所以第一个打印换行由于
\\n
,然后是另一个因为echo
通常附加一个换行符。
The -n
option tells echo
to suppress adding an implicit newline. -n
选项告诉echo
禁止添加隐式换行符。 To get two newlines, you thus need to specify two \\n
. 要获得两个换行符,您需要指定两个
\\n
。
Edit: 编辑:
The problem here is that ssh-keygen
is special. 这里的问题是
ssh-keygen
很特别。 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. 要提供密码(即使是空密码),您需要使用
-P
选项。 Since you then only need one ENTER (for the file path prompt), this command should work: 既然你只需要一个ENTER(对于文件路径提示),这个命令应该工作:
echo | ssh-keygen -P ''
(note the two '
with no space in between: they are important!) (注意两个
'
中间没有空格:它们很重要)
The general solution is to use the yes
command : 一般的解决方案是使用
yes
命令 :
yes '' | command
yes
will repeatedly output the string specified on the command line with a newline appended. yes
将重复输出命令行中指定的字符串,并附加换行符。 If you run it with an empty string as an argument, it'll output an endless string of newlines. 如果以空字符串作为参数运行它,它将输出无限的换行符串。
How about a heredoc with 2 empty lines: 一个有2个空行的heredoc怎么样:
command <<END
END
or use printf 或使用printf
printf "\n\n" | command
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";
Solutions like echo -e "\\n\\n"
will simulate ENTER
by echo empty line only, without shell prompt
.像
echo -e "\\n\\n"
这样的解决方案将仅通过 echo empty line 模拟ENTER
,而没有 shell prompt
。
But if you want to simulate ENTER
with shell prompt
, here is my solution(define a function named entern
):但是如果你想用 shell
prompt
模拟ENTER
,这是我的解决方案(定义一个名为entern
的函数):
zsh: zsh:
entern () {
repeat $1 {echo "${(%%)PS1}"}
}
Bash 4.4+:重击 4.4+:
entern () {
repeat $1 {echo "${PS1@P}"}
}
thanks: https://stackoverflow.com/a/56267057/2752670谢谢: https : //stackoverflow.com/a/56267057/2752670
Use:用:
➜ ~ entern 5
will see:等着瞧:
➜ ~ entern 5
➜ ~
➜ ~
➜ ~
➜ ~
➜ ~
➜ ~
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.