compare a variable and an integer in bash - bash

I have this code. But I always get an error line 34: [24: command not found.
It should execute the code in the if statement as soon as the do while loop executet the 24 time.
#!/bin/bash
input="./user.cvs"
latexStart="\\documentclass[12pt]{article}\\usepackage{labels}\\usepackage{graphicx}\\usepackage{array}\\begin{document}\\graphicspath{{./QRcodes/}}\\newcolumntype{C}{>{\\centering\\arraybackslash} m{27mm} }"
latexEnd="\\end{document}"
latexBeginLabels="\\begin{labels}"
latexEndLabels="\\end{labels}"
counter=0
touch newLabels.txt
echo "$latexStart" >> newLabels.txt
echo "$latexBeginLabels" >> newLabels.txt
while IFS=';' read -r f1 f2 f3 f4 f5 f6 f7 f8 f9 f10 f11 f12 f13
do
path="./QRcodes/$f2$f3.png"
vcard="BEGIN:VCARD%0AN;CHARSET=utf-8:$f3;$f2;;$f1;%0AADR;CHARSET=utf-8;INTL;PARCEL;WORK:;;$f10;$f11;;$f12;$f13%0AEMAIL;INTERNET:$f6%0AORG:$f4%0ATEL;WORK:$f8%0ATEL;FAX;WORK:$f9%0ATITLE:$f5%0AURL;WORK:$f7%0AEND:VCARD"
encodedVCard=$(echo "$vcard" | sed -e 's/\+/\%2B/g')
url="http://api.qrserver.com/v1/create-qr-code/?size=300x300&data=$encodedVCard"
wget -O "$path" "$url"
if ["${counter:-0}" -gt 21] ;
then
counter=0
echo "$latexEndLabels" >> newLabels.txt
echo "\\newpage" >> newLabels.txt
echo "$latexBeginLabels" >> newLabels.txt
fi
echo "\\begin{tabular}{ C C } \\includegraphics[height=30mm]{name.png} & Name Man \\\\ \\end{tabular}" >> newLabels.txt
let counter=counter+1
done < "$input"
echo "$latexEndLabels" >> newLabels.txt
echo "$latexEnd" >> newLabels.txt
The error is in Line 34, if ["${counter:-0}" -gt 21] ;. I got this example from Compare integer in bash, unary operator expected
What am I doing wrong?

You need
if [ "${counter:-0}" -gt 21 ]; then ...
i.e. your conditional statement is separated from the surrounding square brackets by spaces.
I'm quoting counter to be safe, but if you're sure it'll never be empty then you can skip that. It's probably better to be safe than sorry, however, and follow your original pattern.

Related

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.

Bash and Functions

I am to write a basg=h script that displays a message if you input the correct number (i.e. 1 = function f1, 2 = function f2, and 3 = function f3)
My code is as follow
#!/bin/bash
function f1
{
echo "This message is from function 1"
}
function f2
{
echo "This messge is from function 2"
}
function f3
{
echo "This message is from function 3"
}
function=$(typeset -F)
declare -a myarr=(`echo "$function" [sed 's/declare[ ]-f / /g'`)
read -p "Enter a number (1, 2, or 3): " number
if ! [[ "$number" =~ ^[0-9]+$ ]]
then
echo "Not a valid number"
exit
fi
flag=0
for element in "${myarr[#]}
do
if echo "$element" | grep -q "$num"; then
$element
flag=1
fi
done
if [ "$flag" -eq 0 ]; then
echo "No function matches number $num"
fi
Now when I run the code I obtain the error
q6: line 43: unexpected EOF while looking for matching `"'
q6: line 45: syntax error: unexpected end of file
Any help sourcing the errors?
for element in "${myarr[#]}
Missing the end quote. You can catch errors like this by seeing where the syntax highlighting goes wonky. Notice how much of the script following this line is incorrectly colored red?
Even better, you can use ShellCheck:
Line 32:
for element in "${myarr[#]}
^-- SC1009: The mentioned syntax error was in this for loop.
^-- SC1078: Did you forget to close this double quoted string?
Fix that and you get:
Line 1:
#!/bin/bash
^-- SC1114: Remove leading spaces before the shebang.
Line 20:
declare -a myarr=(`echo "$function" [sed 's/declare[ ]-f / /g'`)
^-- SC2207: Prefer mapfile or read -a to split command output (or quote to avoid splitting).
^-- SC2006: Use $(..) instead of legacy `..`.
^-- SC2116: Useless echo? Instead of 'cmd $(echo foo)', just use 'cmd foo'.
Line 22:
read -p "Enter a number (1, 2, or 3): " number
^-- SC2162: read without -r will mangle backslashes.
Line 35:
if echo "$element" | grep -q "$num"; then
^-- SC2154: num is referenced but not assigned.

Simple bash script (input letter output number)

Hi I'm looking to write a simple script which takes an input letter and outputs it's numerical equivalent :-
I was thinking of listing all letters as variables, then have bash read the input as a variable but from here I'm pretty stuck, any help would be awesome!
#!/bin/bash
echo "enter letter"
read "LET"
a=1
b=2
c=3
d=4
e=5
f=6
g=7
h=8
i=9
j=10
k=11
l=12
m=13
n=14
o=15
p=16
q=17
r=18
s=19
t=20
u=21
v=22
w=23
x=24
y=25
z=26
LET=${a..z}
if
$LET = [ ${a..z} ];
then
echo $NUM
sleep 5
echo "success!"
sleep 1
exit
else
echo "FAIL :("
exit
fi
Try this:
echo "Input letter"
read letter
result=$(($(printf "%d\n" \'$letter) - 65))
echo $result
0
ASCII equivalent of 'A' is 65 so all you've got to do to is to take away 65 (or 64, if you want to start with 1, not 0) from the letter you want to check. For lowercase the offset will be 97.
A funny one, abusing Bash's radix system:
read -n1 -p "Type a letter: " letter
if [[ $letter = [[:alpha:]] && $letter = [[:ascii:]] ]]; then
printf "\nCode: %d\n" "$((36#$letter-9))"
else
printf "\nSorry, you didn't enter a valid letter\n"
fi
The interesting part is the $((36#$letter-9)). The 36# part tells Bash to understand the following string as a number in radix 36 which consists of a string containing the digits and letters (case not important, so it'll work with uppercase letters too), with 36#a=10, 36#b=11, …, 36#z=35. So the conversion is just a matter of subtracting 9.
The read -n1 only reads one character from standard input. The [[ $letter = [[:alpha:]] && $letter = [[:ascii:]] ]] checks that letter is really an ascii letter. Without the [[:ascii:]] test, we would validate characters like é (depending on locale) and this would mess up with the conversion.
use these two functions to get chr and ord :
chr() {
[ "$1" -lt 256 ] || return 1
printf "\\$(printf '%03o' "$1")"
}
ord() {
LC_CTYPE=C printf '%d' "'$1"
}
echo $(chr 97)
a
USing od and tr
echo "type letter: "
read LET
echo "$LET" | tr -d "\n" | od -An -t uC
OR using -n
echo -n "$LET" | od -An -t uC
If you want it to start at a=1
echo $(( $(echo -n "$LET" | od -An -t uC) - 96 ))
Explanation
Pipes into the tr to remove the newline.
Use od to change to unsigned decimal.
late to the party: use an associative array:
# require bash version 4
declare -A letters
for letter in {a..z}; do
letters[$letter]=$((++i))
done
read -p "enter a single lower case letter: " letter
echo "the value of $letter is ${letters[$letter]:-N/A}"

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: executing an output of a script as a script

I'm writing a simple script to generate all combinations of a and b of a given length (say 10). I want to be able to do this on a command line (I know this is fairly easy if I just put everything in a bash script file and execute it). However, I was wondering if it's possible to do without any extra files. Here's what I have so far:
n=10;
for i in `seq 1 1 $n`; do
echo "for a$i in {a..b}; do ";
done;
echo -n "echo ";
for i in `seq 1 1 $n`; do
echo -n '$'"a$i"; done;
echo;
for i in `seq 1 1 $n`; do
echo "done;";
done
(I formatted the code for readability, but it's actually all on one line run from a prompt)
This gives me the following output:
for a1 in {a..b}; do
for a2 in {a..b}; do
for a3 in {a..b}; do
for a4 in {a..b}; do
for a5 in {a..b}; do
for a6 in {a..b}; do
for a7 in {a..b}; do
for a8 in {a..b}; do
for a9 in {a..b}; do
for a10 in {a..b}; do
echo $a1$a2$a3$a4$a5$a6$a7$a8$a9$a10
done;
done;
done;
done;
done;
done;
done;
done;
done;
done;
which is just fine. If I copy that and paste it back on the command line, it works like a charm and gives me the result.
The question is how do I do this with just the initial script, without copy-pasting and without redirecting anything to files.
I've tried sticking $( ) around the script, but that gives me "No command 'for' found'", since it's not really a command but a bash builtin. I've tried putting eval somewhere before this, but I just keep getting more errors. I'm a bit stuck, so any help would be greatly appreciated.
(Btw, just to reiterate, I'm doing this more or less to just learn bash more -- that's why I don't want to redirect the output to a file and then execute that file. I know how to do that part, but I don't know how to just do it from command line)
You need to use an eval, $() gives you a string.
eval $( echo echo foo )
Another option is to stick into a subshell and pipe it to a bash:
(echo echo foo) | /bin/bash
You can do for i in $(seq $n) instead of seq 1 1 $n.
You can do for ((i=1; i<=$n; i++)) and avoid calling an external utility.
You can do this (slightly hacky with only one loop):
$ a=A; b=B; n=4; s=''; for ((i=1;i<=n;i++)); do s+="{$a..$b}"; done; eval echo "''" $s"$'\n'"
or this (highly hacky without any loops):
$ a=A; b=B; n=4; eval echo "''" $(printf "{$a..$b}%.0s" $(eval echo "{1..$n}"))"$'\n'"
Either one will get you this:
AAAA
AAAB
AABA
AABB
ABAA
ABAB
ABBA
ABBB
BAAA
BAAB
BABA
BABB
BBAA
BBAB
BBBA
BBBB

Resources