bash scripting - read single keystroke including special keys enter and space
Try setting the read delimiter to an empty string then check the builtin $REPLY variable:
read -d'' -s -n1
For some reason I couldn't get it to work specifying a variable.
Bash Shell Scripting - detect the Enter key
Several issues with the posted code. Inline comments detail what to fix:
#!/bin/bash
# ^^ Bash, not sh, must be used for read options
read -s -n 1 key # -s: do not echo input character. -n 1: read only 1 character (separate with space)
# double brackets to test, single equals sign, empty string for just 'enter' in this case...
# if [[ ... ]] is followed by semicolon and 'then' keyword
if [[ $key = "" ]]; then
echo 'You pressed enter!'
else
echo "You pressed '$key'"
fi
shell script respond to keypress
read -rsn1
Expect only one letter (and don't wait for submitting) and be silent (don't write that letter back).
Bash: detecting key-presses: how to read special key hex sequence(like Insert) and why it is '\x1b\x5b\x32\x7e'(Insert)?
There are several questions asked...
The script is expecting escape sequences to be sent by special keys. On a typical keyboard, those are all of the keys which have a name or graphic symbol (such as ←, for left-cursor). By convention (there is no applicable standard) those keys send sequences of characters beginning with escape, with the second character often [
. For just those two characters, the hexadecimal codes are 0x1b
and 0x5b
, respectively (see ASCII table). The 0x4f
is the letter O
, and would be sent by a terminal's special keys in application mode.
The particular sequences sent by your Home and End keys use a slightly different convention from some of the other special keys, referred to a PC-style (see the xterm FAQ Why can't I use the home/end keys? for the background on that). Apparently the developer of the script decided to solve a problem related to that FAQ by ensuring that both variations of the sequences sent by Home and End would be translated into the VT220-style sequence.
The last question asks why it is necessary to do separate reads and use separate variables. That is because the sequence of characters sent by a special key may be transmitted over a network, and take more time than one of those read
operations allows. So only part of the sequence may be read at each try. The script collects the pieces and puts them together as one sequence of characters.
Bash Read Escaped Characters and Normal Characters
A bit hacky/dirty way, but should work for user interactive shells...
read -n1 -s -p 'input> ' line; read -t 0.1 -n2 -s line2
line="$line$line2"
Now, it's up to you to convert <ESC>[A
to string <UP>
or not.
NOTE: It would most likely fail, if the stdin is redirected (say, from pipe/file...)
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!)
Casing arrow keys in bash
You can use read -n 1
to read one character then use a case
statement to choose an action to take based on the key.
On problem is that arrow keys output more than one character and the sequence (and its length) varies from terminal to terminal.
For example, on the terminal I'm using, the right arrow outputs ^[[C
. You can see what sequence your terminal outputs by pressing Ctrl-V Right Arrow. The same is true for other cursor-control keys such as Page Up and End.
I would recommend, instead, to use single-character keys like <
and >
. Handling them in your script will be much simpler.
read -n 1 key
case "$key" in
'<') go_left;;
'>') go_right;;
esac
Simulating ENTER keypress in bash script
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
Related Topics
How to Boot the Linux Kernel Without Creating an Initrd Image
Mmap: Will the Mapped File Be Loaded into Memory Immediately
What Is File Hole and How Can It Be Used
Why Should I Recompile an Entire Program Just for a Library Update
Using the Universal Chess Interface
Dma Cache Coherence Management
How to Split a Ca Certificate Bundle into Separate Files
How to Diff Top Lines of Two Files Without Intermediate File
Calculate Total Used Disk Space by Files Older Than 180 Days Using Find
Change Filenames to Lowercase in Ubuntu in All Subdirectories
Bash: Get List of Commands Starting with a Given String
200,000 Images in Single Folder in Linux, Performance Issue or Not
Why Can't We Use C Standard Library Functions in Kernel Development
How to Overcome an Incompatibility Between the Ksh on Linux VS. That Installed on Aix/Solaris/Hpux