Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Simulating ENTER keypress in bash script

I've created a really simple bash script that runs a few commands. one of these commands needs user input during runtime. i.e it asks the user "do you want to blah blah blah?", I want to simply send an enter keypress to this so that the script will be completely automated.

I won't have to wait for the input or anything during runtime, its enough to just send the keypress and the input buffer will handle the rest.

like image 496
tobbr Avatar asked Jun 07 '11 11:06

tobbr


People also ask

How do you send enter key in Expect script?

hence typing "\r" for "Enter" key action.

How do you enter in bash?

To check for Bash on your computer, you can type “bash” into your open terminal, like shown below, and hit the enter key. Note that you will only get a message back if the command is not successful. If the command is successful, you will simply see a new line prompt waiting for more input.

How do you enter a shortcut in a bash script?

Here detach takes the place of ctrl + a + d . Sending quit or running exit could often replace ctrl + d . Another work-around would be to use expect which you could then use to send the strings containing control characters or hex values of them.

How do I echo a new line in bash?

Printing Newline in Bash The most common way is to use the echo command. However, the printf command also works fine. Using the backslash character for newline “\n” is the conventional way. However, it's also possible to denote newlines using the “$” sign.


2 Answers

echo -ne '\n' | <yourfinecommandhere> 

or taking advantage of the implicit newline that echo generates (thanks Marcin)

echo | <yourfinecommandhere> 

Now we can simply use the --sk option:

--sk, --skip-keypress Don't wait for a keypress after each test

i.e. sudo rkhunter --sk --checkall

like image 106
Tilman Vogel Avatar answered Sep 24 '22 00:09

Tilman Vogel


You might find the yes command useful.

See man yes

like image 40
pavium Avatar answered Sep 24 '22 00:09

pavium