How to simulate two consecutive ENTER key presses for a command in a bash script?
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!)
Simulating ENTER keypress in bash script
echo -ne '\n' | <yourfinecommandhere>
or taking advantage of the implicit newline that echo generates (thanks Marcin)
echo | <yourfinecommandhere>
How to simulate pressing enter in a bash script
this should work:
echo | ./clientScript.py
it simply outputs a \n
to the stdin of your python script.
How to input automatically when running a shell over SSH?
For general command-line automation, Expect is the classic tool. Or try pexpect if you're more comfortable with Python.
Here's a similar question that suggests using Expect: Use expect in bash script to provide password to SSH command
C prompt user to press enter key and exit after one press
instead of:
while (1) {
printf("press enter to continue \n");
char prompt;
prompt = getchar();
if(prompt == 0x0A){
break;
}
}
You might try (after emptying stdin
do
{
printf("press enter to continue \n");
int prompt = getchar();
} while( prompt != '\n' && prompt != EOF );
How to run a shell script without having to press enter/confirm s.th. inbetween
I finally found an easy and fast way for simulating a key press:
just install xdotool and then use the following code for simulating e.g. the enter key:
import subprocess
subprocess.call(["xdotool","key","Return"])
Related Topics
Identify Program That Connects to a Unix Domain Socket
Why I Am Not Getting Signal Sigkill on Kill -9 Command in Bash
Apache Httpd VS. Tomcat 7: Port 80 VS. Port 8080
How to Run a Windows Docker Container on Linux Host
How to Update Minikube Latest Version
How to Call Makefile Located in Other Directory
Linux Ssh Bash Fork Retry: No Child Processes
Stdin into Zip Command, How to Specify a File Name
Linux Append Console Output to a Logfile
Surprise! The Shell Suggests Command Line Switches
When Using Cpan in Linux Ubuntu Should I Run It Using Sudo/As Root or as My Default User
What's The Purpose of Mmap Memory Protection Prot_None
Linux Service Can't Load Library Path in The /Etc/Ld.So.Conf.D
Bash Script Does Not Continue to Read The Next Line of File
Sigterm Does Not Reach Node Script When Docker Runs It with '/Bin/Sh -C'