How to distinguish whitespace in string after using "read" in bash shell - bash

I'm trying to writing a vim-like editor using shell. And I find that I cannot distinguish whitespace. I read 4 character because some special characters like arrows have 3 bytes. But it will be a mess if you enter keys at a fast speed.
So how can I distinguish space? (If you can tell me a better way to deal with the "entering too fast" problem will be nicer.)
while read -rsN1 key
do
if [ $key==$'\e' ];then
read -sN1 -t 0.0001 k1
read -sN1 -t 0.0001 k2
read -sN1 -t 0.0001 k3
key+=${k1}${k2}${k3}
fi
case $key in
$'\e[A')
echo "up" ;;
' ')
echo "space" ;; #It doesn't work here!!!
i)
echo "insert" ;;
esac
done

You can try this.
Instead of trying that there are a lot of things you can learn when using vim/vi/vim.exe/vi.exe in any operating system (Windows cygwin/mingw, AIX, Linux HP-UX/OSF1/IRIX/SunOS/UNIX)
Hence try learning vi.
However I have updated your code only for my knowledge transfer:
Here goes my coding:
#!/bin/bash
echo "Use any one of the following keys:"
UP=$(echo $'\U2190')
#echo -n $'\U2190' | od -bc
#echo -e "\0342\0206\0220" # LEFT
LEFT=$(echo $'\U2191')
#echo -e "\0342\0206\0221" # UP
RIGHT=$(echo $'\U2192')
#echo -e "\0342\0206\0222" # RIGHT
DOWN=$(echo $'\U2193')
#echo -e "\0342\0206\0223" # DOWN
echo SPACEBAR
while read -rsN1 key
do
if [ $'\e' == "$key" ]
then
read -sN1 -t 0.0001 k1
read -sN1 -t 0.0001 k2
read -sN1 -t 0.0001 k3
key+=${k1}${k2}${k3}
fi
case $key in
$'[A')
echo -n "$UP"
;;
$'[B')
echo -n "$DOWN"
;;
$'[C')
echo -n "$RIGHT"
;;
$'[D')
echo -n "$LEFT"
;;
' ')
echo -n " "
;;
#It doesn't work here!!!
i)
echo "insert"
;;
esac
done
My output:
$ 73172247.sh
Use any one of the following keys:
SPACEBAR
↑↓←→↓↑ ↑↑ ↑ →←↑
Example pdf:
https://www.jics.utk.edu/files/images/csure-reu/PDF-DOC/VI-TUTORIAL.pdf
Textpad.exe/notepad++.exe at windows
gedit at Linux.

Related

How can we delete space with a shell script?

I did a script in order to display different things, in different case.
The script is :
#!/usr/bin/env bash
# declare an array, to store stuff in
declare -a myArray
shopt -s nocasematch
# read the full file into the array
# This while loop terminates when pressing CTRL-D
i=1
while read -r line; do
myArray[i]="${line}"
((i++))
done < /dev/stdin
# Process the array
for ((j=1;j<i;++j)); do
# perform your actions here on myArray[j]
case "${myArray[j]}" in
bob)
echo "boy"
;;
alicia)
echo "girl"
;;
cookie)
echo "dog"
;;
*)
echo "unknown" "${myArray[j]}"
;;
esac
done
But I have a problem, when I execute the code with this command:
cat input.txt | ./prog.sh > file.txt
I have the following input:
bob
alicia
amhed
cookie
daniel
In this input I have so space, but when I run my program I don't obtain this right result. I need my code not to take into account spaces, but if it take care about the space, is wrote "unknown" on the OUTPOUT file.txt
and I obtain the result :
boy
girl
unknown amhed
dog
unknown
unknown
unknown daniel
So can I eliminate/delete the space without touching the input file?
why do this in bash?
with awk
$ awk 'BEGIN{n=split("bob boy alicia girl cookie dog",x);
for(i=1;i<n;i+=2) a[x[i]]=x[i+1]} # build the lookup table
{print $1 in a?a[$1]:"unknown "$1}' file
boy
girl
unknown amhed
dog
unknown
unknown
unknown daniel
you can externalize the lookup map to another file as well, so that the code doesn't need to be modified if either values change.
If you want to do nothing when the input line is empty, you can add that to your case:
#!/usr/bin/env bash
# declare an array, to store stuff in
declare -a myArray
shopt -s nocasematch
# read the full file into the array
# This while loop terminates when pressing CTRL-D
i=1
while read -r line; do
myArray[i]="${line}"
((i++))
done < /dev/stdin
# Process the array
for ((j=1;j<i;++j)); do
# perform your actions here on myArray[j]
case "${myArray[j]}" in
"") # This is an empty line, skip it
;;
bob)
echo "boy"
;;
alicia)
echo "girl"
;;
cookie)
echo "dog"
;;
*)
echo "unknown" "${myArray[j]}"
;;
esac
done
Alternatively, check whether the line you read was empty before adding it to the array.
You can read from stdin and process each line and avoid array creation.
Use && [[ -n $line ]] to make sure you process only non-empty lines.
By default read command reads it from /dev/stdin so you can omit < /dev/stdin from your code.
Code:
shopt -s nocasematch
while read -r line && [[ -n $line ]]; do
case "$line" in
bob)
echo "boy"
;;
alicia)
echo "girl"
;;
cookie)
echo "dog"
;;
*)
echo "unknown $line"
;;
esac
done
Run it as:
./prog.sh < input.txt
Output:
boy
girl
unknown amhed
dog

Bash scripting, using arrow keys [duplicate]

Is it possible to case arrow keys in a bash script to run a certain set of commands if the up/left arrow keys are pressed, and a certain set if the down/right arrow keys are pressed? I am trying to have a way to switch between users quickly by using the arrow keys while it is displaying the data, using this script to read the data from.
function main() # The main function that controls the execution of all other functions
{
mkdir -p ~/usertmp # Make a new temporary user directory if it doesn't exist
touch ~/last_seen_output.txt # Create the output file if it doesn't exist
cat /dev/null > ~/last_seen_output.txt # Make sure that the output file is empty
gather # Call the "gather" function
total=$((`wc -l ~/usertmp/user_list.txt|awk '{print $1}'`-1)) # Calculate the total amount of lines and subtract 1 from the result
echo Current Time: `date +%s` > ~/last_seen_output.txt # Print the current time to the output file for later reference
echo "" > ~/last_seen_output.txt # Print a blank line to the output file
if [ $log -eq 1 ]
then
# If it is enabled, then delete the old backups to prevent errors
while [ $line_number -le $total ]
do
line_number=$((line_number+1)) # Add 1 to the current line number
calculate # Call the "calculate" function
hms # Call the "hms" function to convert the time in seconds to normal time
log
done
else
while [ $line_number -le $total ]
do
line_number=$((line_number+1)) # Add 1 to the current line number
calculate # Call the "calculate" function
hms # Call the "hms" function to convert the time in seconds to normal time
echo "Displaying, please hit enter to view the users one by one."
read # Wait for user input
if [ "$log_while_displaying" ]
then
log
display
else
display
fi
done
fi
}
https://github.com/jbondhus/last-seen/blob/master/last-seen.sh is the complete script.
The read command commented as "wait for user input" is the command that you hit enter to go to the next user for. Basically, what this script does it list users and the time that has passed since each user logged in. I am trying to switch between each user being displayed by using the arrow keys. I figured that it might be possible to use a case statement to case the key input. To reiterate my point, I'm not sure if this is possible. If it isn't, can anyone think of another way to do this?
You can read arrow keys as well as other keys without any unusual commands; you just need to conditionally add a second read call:
escape_char=$(printf "\u1b")
read -rsn1 mode # get 1 character
if [[ $mode == $escape_char ]]; then
read -rsn2 mode # read 2 more chars
fi
case $mode in
'q') echo QUITTING ; exit ;;
'[A') echo UP ;;
'[B') echo DN ;;
'[D') echo LEFT ;;
'[C') echo RIGHT ;;
*) >&2 echo 'ERR bad input'; return ;;
esac
As mentioned before, the cursor keys generate three bytes - and keys like home/end even generate four! A solution I saw somewhere was to let the initial one-char read() follow three subsequent one-char reads with a very short timeout. Most common key sequences can be shown like this f.e.:
#!/bin/bash
for term in vt100 linux screen xterm
{ echo "$term:"
infocmp -L1 $term|egrep 'key_(left|right|up|down|home|end)'
}
Also, /etc/inputrc contains some of these with readline mappings..
So, answering original question, here's a snip from that bash menu i'm just hacking away at:
while read -sN1 key # 1 char (not delimiter), silent
do
# catch multi-char special key sequences
read -sN1 -t 0.0001 k1
read -sN1 -t 0.0001 k2
read -sN1 -t 0.0001 k3
key+=${k1}${k2}${k3}
case "$key" in
i|j|$'\e[A'|$'\e0A'|$'\e[D'|$'\e0D') # cursor up, left: previous item
((cur > 1)) && ((cur--));;
k|l|$'\e[B'|$'\e0B'|$'\e[C'|$'\e0C') # cursor down, right: next item
((cur < $#-1)) && ((cur++));;
$'\e[1~'|$'\e0H'|$'\e[H') # home: first item
cur=0;;
$'\e[4~'|$'\e0F'|$'\e[F') # end: last item
((cur=$#-1));;
' ') # space: mark/unmark item
array_contains ${cur} "${sel[#]}" && \
sel=($(array_remove $cur "${sel[#]}")) \
|| sel+=($cur);;
q|'') # q, carriage return: quit
echo "${sel[#]}" && return;;
esac
draw_menu $cur "${#sel[#]}" "${sel[#]}" "$#" >/dev/tty
cursor_up $#
done
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
# This will bind the arrow keys
while true # Forever = until ctrl+c
do
# r: backslash is not for escape
# s: silent
# "n x": read x chars before returning
read -rsn 1 t
case $t in
A) echo up ;;
B) echo down ;;
C) echo right ;;
D) echo left ;;
esac
done
Not sure if this answer the question directly, but I think it's related - I was wandering where do those codes come from, and I finally found:
Linux Keycode Table (comptechdoc.org)
It's a bit difficult to read at first; for left arrow, lookup "LEFT 4" in the "Key" column, and for the sequence that bash sees, look up the 5th ("keymap" - "normal") column, where it is written as "[D 1b 5b 44" - which are the three bytes (27, 91, 68) representing this key.
Finding the thread How to read arrow keys on really old bash? - The UNIX and Linux Forums, inspired me to write a short one-liner which dumps the key codes of keys pressed. Basically, you press a key, then Enter (to trigger ending of read), and then use hexdump to output what read has saved (and finally hit Ctrl-C to exit the loop):
$ while true; do read -p?; echo -n $REPLY | hexdump -C; done
?^[[D
00000000 1b 5b 44 |.[D| # left arrow
00000003
?^[[C
00000000 1b 5b 43 |.[C| # right arrow
00000003
?^[[1;2D
00000000 1b 5b 31 3b 32 44 |.[1;2D| # Shift+left arrow
00000006
?^[[1;2C
00000000 1b 5b 31 3b 32 43 |.[1;2C| # Shift+right arrow
00000006
?^C
So, while arrow keys require 3 bytes - Shift+arrow keys require 6! However, seemingly all these sequence start with 0x1b (27), so one could possibly check for this value for read -n1, before reading any more bytes; also 5b remains a second byte in multi-byte sequence for the "normal" and "shift/NUM-Lock" columns of the table above.
Edit: much easier and proper way to scan for terminal codes of pressed keys in Linux is via showkey:
$ showkey
Couldn't get a file descriptor referring to the console
$ showkey -h
showkey version 1.15
usage: showkey [options...]
valid options are:
-h --help display this help text
-a --ascii display the decimal/octal/hex values of the keys
-s --scancodes display only the raw scan-codes
-k --keycodes display only the interpreted keycodes (default)
$ sudo showkey -a
Press any keys - Ctrl-D will terminate this program
^[[A 27 0033 0x1b
91 0133 0x5b
65 0101 0x41
^[[B 27 0033 0x1b
91 0133 0x5b
66 0102 0x42
^[[A 27 0033 0x1b
91 0133 0x5b
65 0101 0x41
^[[D 27 0033 0x1b
91 0133 0x5b
68 0104 0x44
^[[C 27 0033 0x1b
91 0133 0x5b
67 0103 0x43
^C 3 0003 0x03
^M 13 0015 0x0d
^D 4 0004 0x04
Using eMPee584 answer I think I came up a good solution for you.
Its output is much the same as user3229933 answer but will not be triggered by shift keys and will work in most terminals.
It has UP DOWN LEFT RIGHT HOME and END Keys
Press 'q' to quit
Most of this is thanks to eMPee584
you may need to change '-sn1' to '-sN1' if you get an error like illegal option n.
#!/bin/bash
while read -sn1 key # 1 char (not delimiter), silent
do
read -sn1 -t 0.0001 k1 # This grabs all three symbols
read -sn1 -t 0.0001 k2 # and puts them together
read -sn1 -t 0.0001 k3 # so you can case their entire input.
key+=${k1}${k2}${k3}
case "$key" in
$'\e[A'|$'\e0A') # up arrow
((cur > 1)) && ((cur--))
echo up;;
$'\e[D'|$'\e0D') # left arrow
((cur > 1)) && ((cur--))
echo left;;
$'\e[B'|$'\e0B') # down arrow
((cur < $#-1)) && ((cur++))
echo down;;
$'\e[C'|$'\e0C') # right arrow
((cur < $#-1)) && ((cur++))
echo right;;
$'\e[1~'|$'\e0H'|$'\e[H') # home key:
cur=0
echo home;;
$'\e[4~'|$'\e0F'|$'\e[F') # end key:
((cur=$#-1))
echo end;;
q) # q: quit
echo Bye!
exit;;
esac
done
To extend JellicleCat's answer:
#!/bin/bash
escape_char=$(printf "\u1b")
read -rsn1 mode # get 1 character
if [[ $mode == $escape_char ]]; then
read -rsn4 -t 0.001 mode # read 2 more chars
fi
case $mode in
'') echo escape ;;
'[a') echo UP ;;
'[b') echo DOWN ;;
'[d') echo LEFT ;;
'[c') echo RIGHT ;;
'[A') echo up ;;
'[B') echo down ;;
'[D') echo left ;;
'[C') echo right ;;
'[2~') echo insert ;;
'[7~') echo home ;;
'[7$') echo HOME ;;
'[8~') echo end ;;
'[8$') echo END ;;
'[3~') echo delete ;;
'[3$') echo DELETE ;;
'[11~') echo F1 ;;
'[12~') echo F2 ;;
'[13~') echo F3 ;;
'[14~') echo F4 ;;
'[15~') echo F5 ;;
'[16~') echo Fx ;;
'[17~') echo F6 ;;
'[18~') echo F7 ;;
'[19~') echo F8 ;;
'[20~') echo F9 ;;
'[21~') echo F10 ;;
'[22~') echo Fy ;;
'[23~') echo F11 ;;
'[24~') echo F12 ;;
'') echo backspace ;;
*) echo $mode;;
esac
None of the above answers have worked for me! I had to grab bits and pieces from many answers in this thread, as well as other searches via google. It took me about an hour to concoct this.
I am running Ubuntu 20.04 LTS and this works for me (although, it may not be perfect, as I had to 'hack' at it):
waitkey() {
local end=""
local key=""
echo
echo " Press ESC ... "
while [ "$end" == "" ]; do
read -rsn1 key
case "$key" in
$'\x1b')
local k=""
# I'm not sure why I have to do this if statement,
# but without it, there are errors. took forever
# to figure out why 'read' would dump me outta the script
if [ "$IFS" ]; then
read -rsn1 -t 0.1 holder && k="$holder"
else
IFS=read -rsn1 -t 0.1 holder && k="$holder"
fi
if [ "$k" == "[" ]; then
read -rsn1 -t 0.1 holder && kk="$holder"
##############################
# you put your arrow code here
#
# eg:
# case "$kk" in
# "A") echo "up arrow!" ;; # do something ...
# esac
##############################
elif [ "$k" == "O" ]; then
read -rsn1 -t 0.1 holder && kk="$holder"
# I am honestly not knowing what this is for
elif [ "$k" == "" ]; then
end=1
fi
esac
done
}
For anyone looking for a Mac compatible version that also handles holding down the shift key as well as enter and space:
#!/bin/bash
ESC=$'\033'
SHIFT=$'[1;2'
# distinguish between enter and space
IFS=''
while true; do
read -rsn1 a
# is the first character ESC?
if [[ $ESC == $a ]]; then
read -rsn2 b
# does SHIFT start with the next two characters?
if [[ $SHIFT == "$b"* ]]; then
read -rsn3 c
fi
fi
input=$a$b$c
unset b c
case $input in
$ESC[A) echo UP ;;
$ESC[B) echo DOWN ;;
$ESC[C) echo RIGHT ;;
$ESC[D) echo LEFT ;;
$ESC$SHIFT'A') echo SHIFT UP ;;
$ESC$SHIFT'B') echo SHIFT DOWN ;;
$ESC$SHIFT'C') echo SHIFT RIGHT ;;
$ESC$SHIFT'D') echo SHIFT LEFT ;;
'') echo ENTER ;;
' ') echo SPACE ;;
q) break ;;
esac
done
Here is yet another variant of readkey for bash.
But first the notes(related to Linux):
read -rsN 1 vs read -rsn 1 - the second variant will not distinguish between escape, enter or space. It's visible from #Paul Hedderly answer.
The tactics:
scanning depends on the first and the third and fifth bytes variations
we scan extended keycodes until we hit "~"/ dec: 126 byte
there are exceptions for third byte dec 49, it should be processed differently
due to way we getting input, it's hard to track ESC button itself, as workaround we doing it, if we getting sequence "27 27", what means 2 presses of ESC button
Notes:
This code should able to return scan codes for any button.
Shift + Key
Alt + Key
keyCode is an array, which would contain the whole button scan sequence
uncomment # echo "Key: ${keyCode[*]}" to see the button scan code
This is not something ideal or perfect, but it works.
Code utilizes bash and could be not compatible with dash, sh, etc
Tested in Linux environment from Windows Terminal ssh.
#!/bin/bash
readKey(){
read -rsN 1 _key
printf %d "'${_key}" # %x for hex
}
demo(){
local keyCode=(0)
while [[ ${keyCode[0]} -ne 10 ]]; do #exit on enter
local keyCode=("$(readKey)") # byte 1
if [[ ${keyCode[0]} -eq 27 ]]; then # escape character
local keyCode+=("$(readKey)") # byte 2
if [[ ${keyCode[-1]} -ne 27 ]]; then # checking if user pressed actual
local keyCode+=("$(readKey)") # byte 3
if [[ "51 50 48 52 53 54" =~ (^|[[:space:]])"${keyCode[2]}"($|[[:space:]]) ]]; then
while [[ ${keyCode[-1]} -ne 126 ]]; do
local keyCode+=("$(readKey)")
done
fi
if [[ "49" =~ (^|[[:space:]])"${keyCode[2]}"($|[[:space:]]) ]]; then
local keyCode+=("$(readKey)") # byte 4
[[ ${keyCode[-1]} -ne 126 ]] && local keyCode+=("$(readKey)") # byte 5
[[ ${keyCode[-1]} -eq 59 ]] && local keyCode+=("$(readKey)") # byte 5 check
[[ ${keyCode[-1]} -ne 126 ]] && local keyCode+=("$(readKey)")
fi
fi
fi
# echo "Key: ${keyCode[*]}"
case "${keyCode[*]}" in
"27 91 65") echo "UP";;
"27 91 66") echo "DOWN";;
"27 91 67") echo "RIGHT";;
"27 91 68") echo "LEFT";;
esac
done
}
demo
Here's a function I made to convert special keys into readable keywords.
For example :
< -> LEFT
Shift + > -> SHIFT_RIGHT
Ctrl + Alt + Home -> CTRL_ALT_HOME
Ctrl + Alt + Shift + Page Up -> CTRL_ALT_SHIFT_PAGEUP
.....
#!/usr/bin/env bash
shopt -s extglob
#keyOf <KEY>
keyOf() {
local key=
local mod=
case "$1" in
$'\177' ) key=BACKSPACE ;;
$'\b' ) key=CTRL_BACKSPACE ;;
$'\E\177' ) key=ALT_BACKSPACE ;;
$'\E\b' ) key=CTRL_ALT_BACKSPACE ;;
$'\t' ) key=TAB ;;
$'\E[Z' ) key=SHIFT_TAB ;;
$'\E' ) key=ESCAPE ;;
' ' ) key=SPACE ;;
esac
if [ -z "${key:+x}" ] && [ "${1:0:1}" = $'\E' ]; then
case "${1#$'\E'}" in
\[??(?)\;2? ) mod=SHIFT_ ;;
\[??(?)\;3? ) mod=ALT_ ;;
\[??(?)\;4? ) mod=ALT_SHIFT_ ;;
\[??(?)\;5? ) mod=CTRL_ ;;
\[??(?)\;6? ) mod=CTRL_SHIFT_ ;;
\[??(?)\;7? ) mod=CTRL_ALT_ ;;
\[??(?)\;8? ) mod=CTRL_ALT_SHIFT_ ;;
esac
case "${1#$'\E'}" in
OA | \[?(1\;?)A ) key="${mod}UP" ;;
OB | \[?(1\;?)B ) key="${mod}DOWN" ;;
OC | \[?(1\;?)C ) key="${mod}RIGHT" ;;
OD | \[?(1\;?)D ) key="${mod}LEFT" ;;
OP | \[?(1\;?)P ) key="${mod}F1" ;;
OQ | \[?(1\;?)Q ) key="${mod}F2" ;;
OR | \[?(1\;?)R ) key="${mod}F3" ;;
OS | \[?(1\;?)S ) key="${mod}F4" ;;
\[15?(\;?)~ ) key="${mod}F5" ;;
\[17?(\;?)~ ) key="${mod}F6" ;;
\[18?(\;?)~ ) key="${mod}F7" ;;
\[19?(\;?)~ ) key="${mod}F8" ;;
\[20?(\;?)~ ) key="${mod}F9" ;;
\[21?(\;?)~ ) key="${mod}F10" ;;
\[23?(\;?)~ ) key="${mod}F11" ;;
\[24?(\;?)~ ) key="${mod}F12" ;;
\[?(?(1)\;?)F ) key="${mod}END" ;;
\[?(?(1)\;?)H ) key="${mod}HOME" ;;
\[2?(\;?)~ ) key="${mod}INSERT" ;;
\[3?(\;?)~ ) key="${mod}DELETE" ;;
\[5?(\;?)~ ) key="${mod}PAGEUP" ;;
\[6?(\;?)~ ) key="${mod}PAGEDOWN" ;;
esac
fi
printf '%s' "${key:-$1}"
}
while read -rsN1 c; do
d=
read -t 0.001 -rsd $'\0' d
c+="$d"
printf "%q \t%q\n" "$c" "$(keyOf "$c")"
done
NOTE: Only tested on Windows (MSYS2/MingW64) but can easily be modified.

Casing arrow keys in bash

Is it possible to case arrow keys in a bash script to run a certain set of commands if the up/left arrow keys are pressed, and a certain set if the down/right arrow keys are pressed? I am trying to have a way to switch between users quickly by using the arrow keys while it is displaying the data, using this script to read the data from.
function main() # The main function that controls the execution of all other functions
{
mkdir -p ~/usertmp # Make a new temporary user directory if it doesn't exist
touch ~/last_seen_output.txt # Create the output file if it doesn't exist
cat /dev/null > ~/last_seen_output.txt # Make sure that the output file is empty
gather # Call the "gather" function
total=$((`wc -l ~/usertmp/user_list.txt|awk '{print $1}'`-1)) # Calculate the total amount of lines and subtract 1 from the result
echo Current Time: `date +%s` > ~/last_seen_output.txt # Print the current time to the output file for later reference
echo "" > ~/last_seen_output.txt # Print a blank line to the output file
if [ $log -eq 1 ]
then
# If it is enabled, then delete the old backups to prevent errors
while [ $line_number -le $total ]
do
line_number=$((line_number+1)) # Add 1 to the current line number
calculate # Call the "calculate" function
hms # Call the "hms" function to convert the time in seconds to normal time
log
done
else
while [ $line_number -le $total ]
do
line_number=$((line_number+1)) # Add 1 to the current line number
calculate # Call the "calculate" function
hms # Call the "hms" function to convert the time in seconds to normal time
echo "Displaying, please hit enter to view the users one by one."
read # Wait for user input
if [ "$log_while_displaying" ]
then
log
display
else
display
fi
done
fi
}
https://github.com/jbondhus/last-seen/blob/master/last-seen.sh is the complete script.
The read command commented as "wait for user input" is the command that you hit enter to go to the next user for. Basically, what this script does it list users and the time that has passed since each user logged in. I am trying to switch between each user being displayed by using the arrow keys. I figured that it might be possible to use a case statement to case the key input. To reiterate my point, I'm not sure if this is possible. If it isn't, can anyone think of another way to do this?
You can read arrow keys as well as other keys without any unusual commands; you just need to conditionally add a second read call:
escape_char=$(printf "\u1b")
read -rsn1 mode # get 1 character
if [[ $mode == $escape_char ]]; then
read -rsn2 mode # read 2 more chars
fi
case $mode in
'q') echo QUITTING ; exit ;;
'[A') echo UP ;;
'[B') echo DN ;;
'[D') echo LEFT ;;
'[C') echo RIGHT ;;
*) >&2 echo 'ERR bad input'; return ;;
esac
As mentioned before, the cursor keys generate three bytes - and keys like home/end even generate four! A solution I saw somewhere was to let the initial one-char read() follow three subsequent one-char reads with a very short timeout. Most common key sequences can be shown like this f.e.:
#!/bin/bash
for term in vt100 linux screen xterm
{ echo "$term:"
infocmp -L1 $term|egrep 'key_(left|right|up|down|home|end)'
}
Also, /etc/inputrc contains some of these with readline mappings..
So, answering original question, here's a snip from that bash menu i'm just hacking away at:
while read -sN1 key # 1 char (not delimiter), silent
do
# catch multi-char special key sequences
read -sN1 -t 0.0001 k1
read -sN1 -t 0.0001 k2
read -sN1 -t 0.0001 k3
key+=${k1}${k2}${k3}
case "$key" in
i|j|$'\e[A'|$'\e0A'|$'\e[D'|$'\e0D') # cursor up, left: previous item
((cur > 1)) && ((cur--));;
k|l|$'\e[B'|$'\e0B'|$'\e[C'|$'\e0C') # cursor down, right: next item
((cur < $#-1)) && ((cur++));;
$'\e[1~'|$'\e0H'|$'\e[H') # home: first item
cur=0;;
$'\e[4~'|$'\e0F'|$'\e[F') # end: last item
((cur=$#-1));;
' ') # space: mark/unmark item
array_contains ${cur} "${sel[#]}" && \
sel=($(array_remove $cur "${sel[#]}")) \
|| sel+=($cur);;
q|'') # q, carriage return: quit
echo "${sel[#]}" && return;;
esac
draw_menu $cur "${#sel[#]}" "${sel[#]}" "$#" >/dev/tty
cursor_up $#
done
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
# This will bind the arrow keys
while true # Forever = until ctrl+c
do
# r: backslash is not for escape
# s: silent
# "n x": read x chars before returning
read -rsn 1 t
case $t in
A) echo up ;;
B) echo down ;;
C) echo right ;;
D) echo left ;;
esac
done
Not sure if this answer the question directly, but I think it's related - I was wandering where do those codes come from, and I finally found:
Linux Keycode Table (comptechdoc.org)
It's a bit difficult to read at first; for left arrow, lookup "LEFT 4" in the "Key" column, and for the sequence that bash sees, look up the 5th ("keymap" - "normal") column, where it is written as "[D 1b 5b 44" - which are the three bytes (27, 91, 68) representing this key.
Finding the thread How to read arrow keys on really old bash? - The UNIX and Linux Forums, inspired me to write a short one-liner which dumps the key codes of keys pressed. Basically, you press a key, then Enter (to trigger ending of read), and then use hexdump to output what read has saved (and finally hit Ctrl-C to exit the loop):
$ while true; do read -p?; echo -n $REPLY | hexdump -C; done
?^[[D
00000000 1b 5b 44 |.[D| # left arrow
00000003
?^[[C
00000000 1b 5b 43 |.[C| # right arrow
00000003
?^[[1;2D
00000000 1b 5b 31 3b 32 44 |.[1;2D| # Shift+left arrow
00000006
?^[[1;2C
00000000 1b 5b 31 3b 32 43 |.[1;2C| # Shift+right arrow
00000006
?^C
So, while arrow keys require 3 bytes - Shift+arrow keys require 6! However, seemingly all these sequence start with 0x1b (27), so one could possibly check for this value for read -n1, before reading any more bytes; also 5b remains a second byte in multi-byte sequence for the "normal" and "shift/NUM-Lock" columns of the table above.
Edit: much easier and proper way to scan for terminal codes of pressed keys in Linux is via showkey:
$ showkey
Couldn't get a file descriptor referring to the console
$ showkey -h
showkey version 1.15
usage: showkey [options...]
valid options are:
-h --help display this help text
-a --ascii display the decimal/octal/hex values of the keys
-s --scancodes display only the raw scan-codes
-k --keycodes display only the interpreted keycodes (default)
$ sudo showkey -a
Press any keys - Ctrl-D will terminate this program
^[[A 27 0033 0x1b
91 0133 0x5b
65 0101 0x41
^[[B 27 0033 0x1b
91 0133 0x5b
66 0102 0x42
^[[A 27 0033 0x1b
91 0133 0x5b
65 0101 0x41
^[[D 27 0033 0x1b
91 0133 0x5b
68 0104 0x44
^[[C 27 0033 0x1b
91 0133 0x5b
67 0103 0x43
^C 3 0003 0x03
^M 13 0015 0x0d
^D 4 0004 0x04
Using eMPee584 answer I think I came up a good solution for you.
Its output is much the same as user3229933 answer but will not be triggered by shift keys and will work in most terminals.
It has UP DOWN LEFT RIGHT HOME and END Keys
Press 'q' to quit
Most of this is thanks to eMPee584
you may need to change '-sn1' to '-sN1' if you get an error like illegal option n.
#!/bin/bash
while read -sn1 key # 1 char (not delimiter), silent
do
read -sn1 -t 0.0001 k1 # This grabs all three symbols
read -sn1 -t 0.0001 k2 # and puts them together
read -sn1 -t 0.0001 k3 # so you can case their entire input.
key+=${k1}${k2}${k3}
case "$key" in
$'\e[A'|$'\e0A') # up arrow
((cur > 1)) && ((cur--))
echo up;;
$'\e[D'|$'\e0D') # left arrow
((cur > 1)) && ((cur--))
echo left;;
$'\e[B'|$'\e0B') # down arrow
((cur < $#-1)) && ((cur++))
echo down;;
$'\e[C'|$'\e0C') # right arrow
((cur < $#-1)) && ((cur++))
echo right;;
$'\e[1~'|$'\e0H'|$'\e[H') # home key:
cur=0
echo home;;
$'\e[4~'|$'\e0F'|$'\e[F') # end key:
((cur=$#-1))
echo end;;
q) # q: quit
echo Bye!
exit;;
esac
done
To extend JellicleCat's answer:
#!/bin/bash
escape_char=$(printf "\u1b")
read -rsn1 mode # get 1 character
if [[ $mode == $escape_char ]]; then
read -rsn4 -t 0.001 mode # read 2 more chars
fi
case $mode in
'') echo escape ;;
'[a') echo UP ;;
'[b') echo DOWN ;;
'[d') echo LEFT ;;
'[c') echo RIGHT ;;
'[A') echo up ;;
'[B') echo down ;;
'[D') echo left ;;
'[C') echo right ;;
'[2~') echo insert ;;
'[7~') echo home ;;
'[7$') echo HOME ;;
'[8~') echo end ;;
'[8$') echo END ;;
'[3~') echo delete ;;
'[3$') echo DELETE ;;
'[11~') echo F1 ;;
'[12~') echo F2 ;;
'[13~') echo F3 ;;
'[14~') echo F4 ;;
'[15~') echo F5 ;;
'[16~') echo Fx ;;
'[17~') echo F6 ;;
'[18~') echo F7 ;;
'[19~') echo F8 ;;
'[20~') echo F9 ;;
'[21~') echo F10 ;;
'[22~') echo Fy ;;
'[23~') echo F11 ;;
'[24~') echo F12 ;;
'') echo backspace ;;
*) echo $mode;;
esac
None of the above answers have worked for me! I had to grab bits and pieces from many answers in this thread, as well as other searches via google. It took me about an hour to concoct this.
I am running Ubuntu 20.04 LTS and this works for me (although, it may not be perfect, as I had to 'hack' at it):
waitkey() {
local end=""
local key=""
echo
echo " Press ESC ... "
while [ "$end" == "" ]; do
read -rsn1 key
case "$key" in
$'\x1b')
local k=""
# I'm not sure why I have to do this if statement,
# but without it, there are errors. took forever
# to figure out why 'read' would dump me outta the script
if [ "$IFS" ]; then
read -rsn1 -t 0.1 holder && k="$holder"
else
IFS=read -rsn1 -t 0.1 holder && k="$holder"
fi
if [ "$k" == "[" ]; then
read -rsn1 -t 0.1 holder && kk="$holder"
##############################
# you put your arrow code here
#
# eg:
# case "$kk" in
# "A") echo "up arrow!" ;; # do something ...
# esac
##############################
elif [ "$k" == "O" ]; then
read -rsn1 -t 0.1 holder && kk="$holder"
# I am honestly not knowing what this is for
elif [ "$k" == "" ]; then
end=1
fi
esac
done
}
For anyone looking for a Mac compatible version that also handles holding down the shift key as well as enter and space:
#!/bin/bash
ESC=$'\033'
SHIFT=$'[1;2'
# distinguish between enter and space
IFS=''
while true; do
read -rsn1 a
# is the first character ESC?
if [[ $ESC == $a ]]; then
read -rsn2 b
# does SHIFT start with the next two characters?
if [[ $SHIFT == "$b"* ]]; then
read -rsn3 c
fi
fi
input=$a$b$c
unset b c
case $input in
$ESC[A) echo UP ;;
$ESC[B) echo DOWN ;;
$ESC[C) echo RIGHT ;;
$ESC[D) echo LEFT ;;
$ESC$SHIFT'A') echo SHIFT UP ;;
$ESC$SHIFT'B') echo SHIFT DOWN ;;
$ESC$SHIFT'C') echo SHIFT RIGHT ;;
$ESC$SHIFT'D') echo SHIFT LEFT ;;
'') echo ENTER ;;
' ') echo SPACE ;;
q) break ;;
esac
done
Here is yet another variant of readkey for bash.
But first the notes(related to Linux):
read -rsN 1 vs read -rsn 1 - the second variant will not distinguish between escape, enter or space. It's visible from #Paul Hedderly answer.
The tactics:
scanning depends on the first and the third and fifth bytes variations
we scan extended keycodes until we hit "~"/ dec: 126 byte
there are exceptions for third byte dec 49, it should be processed differently
due to way we getting input, it's hard to track ESC button itself, as workaround we doing it, if we getting sequence "27 27", what means 2 presses of ESC button
Notes:
This code should able to return scan codes for any button.
Shift + Key
Alt + Key
keyCode is an array, which would contain the whole button scan sequence
uncomment # echo "Key: ${keyCode[*]}" to see the button scan code
This is not something ideal or perfect, but it works.
Code utilizes bash and could be not compatible with dash, sh, etc
Tested in Linux environment from Windows Terminal ssh.
#!/bin/bash
readKey(){
read -rsN 1 _key
printf %d "'${_key}" # %x for hex
}
demo(){
local keyCode=(0)
while [[ ${keyCode[0]} -ne 10 ]]; do #exit on enter
local keyCode=("$(readKey)") # byte 1
if [[ ${keyCode[0]} -eq 27 ]]; then # escape character
local keyCode+=("$(readKey)") # byte 2
if [[ ${keyCode[-1]} -ne 27 ]]; then # checking if user pressed actual
local keyCode+=("$(readKey)") # byte 3
if [[ "51 50 48 52 53 54" =~ (^|[[:space:]])"${keyCode[2]}"($|[[:space:]]) ]]; then
while [[ ${keyCode[-1]} -ne 126 ]]; do
local keyCode+=("$(readKey)")
done
fi
if [[ "49" =~ (^|[[:space:]])"${keyCode[2]}"($|[[:space:]]) ]]; then
local keyCode+=("$(readKey)") # byte 4
[[ ${keyCode[-1]} -ne 126 ]] && local keyCode+=("$(readKey)") # byte 5
[[ ${keyCode[-1]} -eq 59 ]] && local keyCode+=("$(readKey)") # byte 5 check
[[ ${keyCode[-1]} -ne 126 ]] && local keyCode+=("$(readKey)")
fi
fi
fi
# echo "Key: ${keyCode[*]}"
case "${keyCode[*]}" in
"27 91 65") echo "UP";;
"27 91 66") echo "DOWN";;
"27 91 67") echo "RIGHT";;
"27 91 68") echo "LEFT";;
esac
done
}
demo
Here's a function I made to convert special keys into readable keywords.
For example :
< -> LEFT
Shift + > -> SHIFT_RIGHT
Ctrl + Alt + Home -> CTRL_ALT_HOME
Ctrl + Alt + Shift + Page Up -> CTRL_ALT_SHIFT_PAGEUP
.....
#!/usr/bin/env bash
shopt -s extglob
#keyOf <KEY>
keyOf() {
local key=
local mod=
case "$1" in
$'\177' ) key=BACKSPACE ;;
$'\b' ) key=CTRL_BACKSPACE ;;
$'\E\177' ) key=ALT_BACKSPACE ;;
$'\E\b' ) key=CTRL_ALT_BACKSPACE ;;
$'\t' ) key=TAB ;;
$'\E[Z' ) key=SHIFT_TAB ;;
$'\E' ) key=ESCAPE ;;
' ' ) key=SPACE ;;
esac
if [ -z "${key:+x}" ] && [ "${1:0:1}" = $'\E' ]; then
case "${1#$'\E'}" in
\[??(?)\;2? ) mod=SHIFT_ ;;
\[??(?)\;3? ) mod=ALT_ ;;
\[??(?)\;4? ) mod=ALT_SHIFT_ ;;
\[??(?)\;5? ) mod=CTRL_ ;;
\[??(?)\;6? ) mod=CTRL_SHIFT_ ;;
\[??(?)\;7? ) mod=CTRL_ALT_ ;;
\[??(?)\;8? ) mod=CTRL_ALT_SHIFT_ ;;
esac
case "${1#$'\E'}" in
OA | \[?(1\;?)A ) key="${mod}UP" ;;
OB | \[?(1\;?)B ) key="${mod}DOWN" ;;
OC | \[?(1\;?)C ) key="${mod}RIGHT" ;;
OD | \[?(1\;?)D ) key="${mod}LEFT" ;;
OP | \[?(1\;?)P ) key="${mod}F1" ;;
OQ | \[?(1\;?)Q ) key="${mod}F2" ;;
OR | \[?(1\;?)R ) key="${mod}F3" ;;
OS | \[?(1\;?)S ) key="${mod}F4" ;;
\[15?(\;?)~ ) key="${mod}F5" ;;
\[17?(\;?)~ ) key="${mod}F6" ;;
\[18?(\;?)~ ) key="${mod}F7" ;;
\[19?(\;?)~ ) key="${mod}F8" ;;
\[20?(\;?)~ ) key="${mod}F9" ;;
\[21?(\;?)~ ) key="${mod}F10" ;;
\[23?(\;?)~ ) key="${mod}F11" ;;
\[24?(\;?)~ ) key="${mod}F12" ;;
\[?(?(1)\;?)F ) key="${mod}END" ;;
\[?(?(1)\;?)H ) key="${mod}HOME" ;;
\[2?(\;?)~ ) key="${mod}INSERT" ;;
\[3?(\;?)~ ) key="${mod}DELETE" ;;
\[5?(\;?)~ ) key="${mod}PAGEUP" ;;
\[6?(\;?)~ ) key="${mod}PAGEDOWN" ;;
esac
fi
printf '%s' "${key:-$1}"
}
while read -rsN1 c; do
d=
read -t 0.001 -rsd $'\0' d
c+="$d"
printf "%q \t%q\n" "$c" "$(keyOf "$c")"
done
NOTE: Only tested on Windows (MSYS2/MingW64) but can easily be modified.

Bash read backspace button behavior problem

When using read in bash, pressing backspace does not delete the last character entered, but appears to append a backspace to the input buffer. Is there any way I can change it so that delete removes the last key typed from the input? If so how?
Here's a short example prog I'm using it with if it's of any help:
#!/bin/bash
colour(){ #$1=text to colourise $2=colour id
printf "%s%s%s" $(tput setaf $2) "$1" $(tput sgr0)
}
game_over() { #$1=message $2=score
printf "\n%s\n%s\n" "$(colour "Game Over!" 1)" "$1"
printf "Your score: %s\n" "$(colour $2 3)"
exit 0
}
score=0
clear
while true; do
word=$(shuf -n1 /usr/share/dict/words) #random word from dictionary
word=${word,,} #to lower case
len=${#word}
let "timeout=(3+$len)/2"
printf "%s (time %s): " "$(colour $word 2)" "$(colour $timeout 3)"
read -t $timeout -n $len input #read input here
if [ $? -ne 0 ]; then
game_over "You did not answer in time" $score
elif [ "$input" != "$word" ]; then
game_over "You did not type the word correctly" $score;
fi
printf "\n"
let "score+=$timeout"
done
The option -n nchars turns the terminal into raw mode, so your best chance is to rely on readline (-e) [docs]:
$ read -n10 -e VAR
BTW, nice idea, although I would leave the end of the word to the user (it's a knee-jerk reaction to press return).
I know the post is old, still this can be useful for someone. If you need specific response to a single keypress on backspace, something like this can do it (without -e):
backspace=$(cat << eof
0000000 005177
0000002
eof
)
read -sn1 hit
[[ $(echo "$hit" | od) = "$backspace" ]] && echo -e "\nDo what you want\n"

Bash Menu: Return to menu after selection made and executed?

I've got a bash script that writes to a file. at the end of the script I want to display a menu, with line numbers - and have the user be able to select 1 or 2, (etc. up to the number of lines in the file) then have it execute that line.
Up to here is perfect.
However, after the line is executed (say for example it displays another file.) I'd like to return to the menu and let the user select another number. Including zero for exiting the menu.
Once the menu is displayed I have the following. (dumpline being the line of the file read)
dresult=`sed -n "$dumpline"p "$PWD"/"$myday1"_"$myjob".txt`
$dresult
But right now - after running the variable $dresult - it exits the shell (where instead I'd like the menu displayed.
Any thoughts?
thank you in advance.
Here's another way to do a menu which relies on cat having the ability to number the lines of a file (some versions of cat may not have that - see the second example if this is the case). Both examples are for a simple four-item menu:
while [[ 1 ]]
do
cat -n menufile
read -p "Make a selection " choice
case $choice in
1|2)
echo "A or B"
;;
3)
echo "C"
;;
4)
break
;;
*)
echo "Invalid choice"
;;
esac
done
This doesn't require cat -n:
saveIFS="$IFS"
IFS=$'\n'
read -d '' -a menuarray < menufile
IFS="$saveIFS"
for (( i=0; i<${#menuarray[#]}; i++ ))
do
menu=$menu"$(($i+1))) ${menuarray[i]}"$'\n'
done
while [[ 1 ]]
do
echo "$menu"
read -p "Make a selection " choice
case $choice in
1|2)
echo "A or B"
;;
3)
echo "C"
;;
4)
break
;;
*)
echo "Invalid choice"
;;
esac
done
My comments on dz's answer are too long for a comment, so I'm posting them here:
Using seq with select would make a redundant-looking menu, with no correlation between it and the display of the lines in $dumpfile:
ls
echo 'hello'
1) 1
2) 2
etc.
You could do something like this instead:
saveIFS=$IFS
IFS=$'\n'
menu=$(< $dumpfile)
PS3="Make a selection: "
select ACTION in $menu QUIT
do
IFS=$saveIFS
case ...
I think, you need something like a loop. Here is a small skelleton for executing a selected line from file:
#!/bin/bash
dumpfile="bla.txt"
echo "ls
echo 'hello'" > ${dumpfile}
function do_action {
line="$( sed -n "${1},1p" "$dumpfile" )"
(
eval "${line}"
)
}
cat -n $dumpfile
nr=$( cat "${dumpfile}" | wc -l )
PS3="select line nr or nr for QUIT: "
select ACTION in $( seq "$nr" ) QUIT
do
case $ACTION in
QUIT) echo "exit" ; break ;; #EXIT
*) do_action "$ACTION" ;;
esac
done
But be aware of the following:
Using eval might be not allways be a good idea (escaping is hard). Sometimes $line should be sufficient.
Using a subshell prevents changing variables by executing a line of the file. It also does prevent exiting the script from lines that do normally exits a shell.

Resources