How to Simulate Two Consecutive Enter Key Presses for a Command in a Bash Script

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



Leave a reply



Submit