ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
I'm stuck with a tiny problem (I hope).
I'm creating a bash script to launch different softwares via a single keyboard touch. For exemple in my script I did this :
Code:
read -s -n1 answer
case $answer in
$'\e')
echo "Kodi is Starting !"
exec kodi
;;
$'a')
echo "Emulation Station is Starting !"
exec emulationstation
;;
esac
exit 0;
Where pressing Escape launch Kodi and where the key "a" launch emulationstation. This is working just fine, now the question is, how to use the backspace key ?
I tried $'\b') but unfortunately this isn't working.
Like :
Code:
$'\b')
echo "Boot !"
exec startx
;;
For the people who are wondering why I need this, it is because I won't have any keyboard but Buttons I soldered to the GPIO. I want to be able to start some soft directly from the console by pressing one button, which are actually keyboard key.
Backspace key can generate 0x08 or 0x7f -- the last fifty year wasn't enough to standardize this. (Or at least to make linux-distributors to follow terminfo instead of hacking it.)
in my test file I did using some code from someone else just to see what it'd get me, when checking it no matter what whenever I hit my 'backspace' that is all that it took to get that '/b') case to work.
Code:
#!/bin/bash
#read -n 1 x; while read -n 1 -t .1 y; do x="$x$y"; done
read -s -n1 answer
case $answer in
$'\e')
echo "answer is esc : $answer"
;;
$'a')
echo "answer is a : $answer"
;;
$'\b')
{
echo "answer is ?: $answer"
rr=( $(echo -n $answer | od -x) )
echo ${rr[1]}
}
;;
*)
echo "default answer is : $answer"
;;
esac
exit 0
results
Code:
userx%slackwhere ⚡ testing ⚡> ./Testing-Keystrokes
answer is ?:
0008
Great ! The $'\x7f' works like a charm ! Fantastic.
But I have a other issue tho...
I tried my bash script on my console and it doesn't start. Is it not working on a raspbian ?
Code:
#!/bin/bash
clear
echo -e "Welcome\x1b[31m Asso'\x1b[0m : $(date)"
echo -e "---------------------------------"
echo -e "- Press TRIANGLE ▲ - Kodi"
echo -e "- Press SQUARE ■ - EmulationStation"
echo -e "- Press CIRCLE \033[1mO\033[0m - StartX"
echo -e
echo -e "- Press Any Key - Back to shell"
echo -e "---------------------------------"
echo -e
read -s -N 1 answer
if [ "$answer" == $'\e' ]; then
echo "Kodi is Starting !"
kodi
elif [ "$answer" = $'a' ]; then
echo "EmulationStation is Starting"
emulationstation
elif [ "$answer" == $'\x7f' ]; then
echo "StartX is Starting"
startx
else
clear
echo "Back to Shell !"
exit
fi
Code:
Several errors with
the read answer line 13
s'\r' (even tho I don't have any R anywhere...) line 15
'elif [ "$answer" = $'a' ]; then line 19
I did something wrong ? It's just a bash after all. It is located in the boot, is it a problem ?
EDIT : Oh yeah I went from "case" back to "if"... Which one is the best ?
Great ! The $'\x7f' works like a charm ! Fantastic.
But I have a other issue tho...
I tried my bash script on my console and it doesn't start. Is it not working on a raspbian ?
Code:
Several errors with
the read answer line 13
s'\r' (even tho I don't have any R anywhere...) line 15
'elif [ "$answer" = $'a' ]; then line 19
I did something wrong ? It's just a bash after all. It is located in the boot, is it a problem ?
EDIT : Oh yeah I went from "case" back to "if"... Which one is the best ?
case vs if
programmer preference?
if you do a case *) default would take the place of else line, but I'd put a loop error handling just in case you hit the wrong one then repeat a message read again.
what are the error for that line?
its working for me.
Several errors with
the read answer line 13
s'\r' (even tho I don't have any R anywhere...) line 15
'elif [ "$answer" = $'a' ]; then line 19
I did something wrong ? It's just a bash after all. It is located in the boot, is it a problem ?
Works for me. It looks like maybe you have a hidden carriage return in your code? What were you using to create/edit the file? Was Windows involved at any stage?
Last edited by suicidaleggroll; 06-27-2017 at 12:40 PM.
Works for me. It looks like maybe you have a hidden carriage return in your code? What were you using to create/edit the file? Was Windows involved at any stage?
Correct I saved the bash on a windows via notepad ++. I will try to copy the batch from linux to linux.
I created the bash on debian and tested it there (virtual machine). I copy the content of the file on windows (notepad ++) and copy the file to the SD card. Maybe something has been messed up during the transfer even if the content of the file is similar.
@BW-userx You are talking about a loop, do you have a exemple for me so I understand what you mean ?
EDIT : A other thing, when I use Up, Down, Left or Right it is starting kodi. Any idea why ?
The arrow keys actually output more then one byte which is why you see the [c on the command line. You can use the showkey command to output the values. Using single value keys would make it easier to get your program working.
When the backspace key is pressed some terminals send ^H (\b) others send DEL (0x7f). To make your script work with both configurations of terminal you can do something like this:
Code:
while read -s -r -n1
do
case "$REPLY" in
$(tput kbs) ) echo "You pressed the backspace key" ;;
a) echo "You pressed 'a'" ;;
*) echo "You pressed something else" ;;
esac
done
Of course, this relies on both your terminal using the correct $TERM setting, and the terminfo entry for that terminal having the correct value for KBS, which is not always the case.
The above is arguably the more correct approach, but given how common it is to find misconfigured terminals, you might just be better off doing this:
Code:
while read -s -r -n1
do
case "$REPLY" in
$'\b' | $'\x7f' ) echo "You pressed the backspace key" ;;
a) echo "You pressed 'a'" ;;
*) echo "You pressed something else" ;;
esac
done
Also, be aware that many keys like cursor/function keys may generate multiple characters when pressed and not just one, so using read -n1 is going to be fragile at the best of times.
BTW, you can read/parse the escape sequences, but it's a little more complex as you have to assemble them with a nested read and using a timeout to determine where they end (this is exactly how ncurses works internally).
Code:
#!/bin/bash
while read -s -r -n1
do
case "$REPLY" in
$'\b' | $'\x7f' ) echo "You pressed the backspace key" ;;
a) echo "You pressed 'a'" ;;
$'\e' ) seq="$REPLY"
while read -s -r -n1 -t ${ESCDELAY:-0.1}
do
seq="$seq$REPLY"
done
case "$seq" in
$'\e') echo "You pressed escape" ;;
$'\e'[D ) echo "You pressed left" ;;
$'\e'[C ) echo "You pressed right" ;;
esac
seq=""
;;
*) echo "You pressed something else" ;;
esac
done
If you're on a slow connection you might need to increase ESCDELAY.
Last edited by GazL; 06-27-2017 at 05:32 PM.
Reason: Added comment about ESCDELAY
There is my code and you guys where right, from linux to linux it's working well. Except that it doesnt launch emulationstation for some reason
It says : emulationstation should not be run as root. If you used 'sudo emulationstation' please run without sudo.
Thing is, and as you can see in the code, I'm not running sudo and the user here is pi not root. So I don't get it, anyone to help me finalize this ?
Thanks guys !!!
Code:
#!/bin/bash
clear
echo -e "Welcome\x1b[31m Asso'\x1b[0m : $(date)"
echo -e
echo -e "-------------------------------------"
echo -e "- Press TRIANGLE ▲ - Kodi"
echo -e "- Press SQUARE ■ - EmulationStation"
echo -e "- Press CIRCLE \033[1mO\033[0m - StartX"
echo -e
echo -e "- Press Any Key - Back to shell"
echo -e "-------------------------------------"
echo -e
read -s -N 1 answer
if [ "$answer" == $'\e' ]; then
echo "Kodi is Starting !"
kodi
elif [ "$answer" = $'a' ]; then
echo "EmulationStation is Starting"
emulationstation
elif [ "$answer" == $'\x7f' ]; then
echo "StartX is Starting"
startx
else
clear
echo "Back to Shell !"
exit
fi
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.