LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 06-27-2017, 08:24 AM   #1
Assomnia
Member
 
Registered: May 2017
Posts: 38

Rep: Reputation: Disabled
"Case" in bash script


Hello all,

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.

Thanks !
 
Old 06-27-2017, 08:56 AM   #2
pan64
LQ Addict
 
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 21,792

Rep: Reputation: 7306Reputation: 7306Reputation: 7306Reputation: 7306Reputation: 7306Reputation: 7306Reputation: 7306Reputation: 7306Reputation: 7306Reputation: 7306Reputation: 7306
ok, probably not nice, but I think it will work:
Code:
#!/bin/bash
read -s -n1 answer
rr=( $(echo -n $answer | od -x) )
echo ${rr[1]}
this is a small test, you can see all the keypresses. You need to use the printed result in case.
Code:
 case ${rr[1]} in
        007f )              # this is the backspace
 
Old 06-27-2017, 09:53 AM   #3
NevemTeve
Senior Member
 
Registered: Oct 2011
Location: Budapest
Distribution: Debian/GNU/Linux, AIX
Posts: 4,856
Blog Entries: 1

Rep: Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869
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.)
 
1 members found this post helpful.
Old 06-27-2017, 11:57 AM   #4
Assomnia
Member
 
Registered: May 2017
Posts: 38

Original Poster
Rep: Reputation: Disabled
Thanks guys I will try that out !
 
Old 06-27-2017, 12:04 PM   #5
BW-userx
LQ Guru
 
Registered: Sep 2013
Location: Somewhere in my head.
Distribution: Slackware (15 current), Slack15, Ubuntu studio, MX Linux, FreeBSD 13.1, WIn10
Posts: 10,342

Rep: Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242
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
so I do wonder why it is not just working .

Last edited by BW-userx; 06-27-2017 at 12:06 PM.
 
Old 06-27-2017, 12:08 PM   #6
Assomnia
Member
 
Registered: May 2017
Posts: 38

Original Poster
Rep: Reputation: Disabled
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 ?

Last edited by Assomnia; 06-27-2017 at 12:09 PM.
 
Old 06-27-2017, 12:20 PM   #7
BW-userx
LQ Guru
 
Registered: Sep 2013
Location: Somewhere in my head.
Distribution: Slackware (15 current), Slack15, Ubuntu studio, MX Linux, FreeBSD 13.1, WIn10
Posts: 10,342

Rep: Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242
Quote:
Originally Posted by Assomnia View Post
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.

Last edited by BW-userx; 06-27-2017 at 12:30 PM.
 
Old 06-27-2017, 12:37 PM   #8
suicidaleggroll
LQ Guru
 
Registered: Nov 2010
Location: Colorado
Distribution: OpenSUSE, CentOS
Posts: 5,573

Rep: Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142
Quote:
Originally Posted by Assomnia View Post
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 ?
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.
 
Old 06-27-2017, 03:36 PM   #9
Assomnia
Member
 
Registered: May 2017
Posts: 38

Original Poster
Rep: Reputation: Disabled
Quote:
Originally Posted by suicidaleggroll View Post
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 ?

Last edited by Assomnia; 06-27-2017 at 03:38 PM.
 
Old 06-27-2017, 03:52 PM   #10
NevemTeve
Senior Member
 
Registered: Oct 2011
Location: Budapest
Distribution: Debian/GNU/Linux, AIX
Posts: 4,856
Blog Entries: 1

Rep: Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869
Google for ESCape-sequences.
 
Old 06-27-2017, 04:05 PM   #11
BW-userx
LQ Guru
 
Registered: Sep 2013
Location: Somewhere in my head.
Distribution: Slackware (15 current), Slack15, Ubuntu studio, MX Linux, FreeBSD 13.1, WIn10
Posts: 10,342

Rep: Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242
Code:
#!/bin/bash


while [[ true ]] 
do

echo "enter something" 
read -s -n1 answer

case $answer in
$'\e')
      echo "answer is esc: $answer"
      rr=( $(echo -n $answer | od -x) )
      echo ${rr[1]}
      break;
     ;;
$'a')
    echo "answer is a : $answer"
    break;
    ;;
$'\b')
{ #backspace key
      echo "answer is ?: $answer"
      rr=( $(echo -n $answer | od -x) )
      echo ${rr[1]}
      break;
  }
 ;;
      *)
	echo "Yooou Screwed up try again: $answer"
      ;;
esac

done
on my keyboard I get the same key code for esc and arrows.


esc key press got this
Code:
userx%slackwhere ⚡ testing ⚡> ./keep-asking
enter something
answer is esc: 
               001b
arrow key press got this
Code:
userx%slackwhere ⚡ testing ⚡> ./keep-asking
enter something
answer is esc: 
               001b
userx%slackwhere ⚡ testing ⚡> [C
Using that (posted) script.
 
Old 06-27-2017, 04:40 PM   #12
michaelk
Moderator
 
Registered: Aug 2002
Posts: 25,678

Rep: Reputation: 5892Reputation: 5892Reputation: 5892Reputation: 5892Reputation: 5892Reputation: 5892Reputation: 5892Reputation: 5892Reputation: 5892Reputation: 5892Reputation: 5892
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.

Here are some ideas.
https://stackoverflow.com/questions/...-bash#11759139
https://unix.stackexchange.com/quest...-being-pressed
 
1 members found this post helpful.
Old 06-27-2017, 04:48 PM   #13
BW-userx
LQ Guru
 
Registered: Sep 2013
Location: Somewhere in my head.
Distribution: Slackware (15 current), Slack15, Ubuntu studio, MX Linux, FreeBSD 13.1, WIn10
Posts: 10,342

Rep: Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242
Here's a loop and showing you how to use a function for a bit of fun, still does not fix your arrow issue though -- but still good to learn from.
Code:
#!/bin/bash

#set -x

message1()
{
read -s -n1 -p "Welcome $USER : $(date)
        ---------------------------------
	- Press TRIANGLE ▲ - Kodi
	- Press SQUARE   ■ - EmulationStation
	- Press CIRCLE   \033[1mO\033[0m - StartX

	- Press Any other Key - See what Happens
	---------------------------------
" answer
 #return value
 key=$answer
}

#function call
message1

while [[ true ]] 
do

case $key in

$'\e')
      echo "answer is esc: $answer"
      rr=( $(echo -n $answer | od -x) )
      echo ${rr[1]}
      break;
     ;;
$'a')
    echo "answer is a : $answer"
    break;
    ;;
$'\b')
{ #backspace key
      echo "answer is ?: $answer"
      rr=( $(echo -n $answer | od -x) )
      echo ${rr[1]}
      break;
  }
 ;;
      *)
      {
	echo "Yooou Screwed up see --> : $answer
	      now try it again Jack O'"
	message1
	}
      ;;
esac

done

Last edited by BW-userx; 06-27-2017 at 05:00 PM.
 
Old 06-27-2017, 04:53 PM   #14
GazL
LQ Veteran
 
Registered: May 2008
Posts: 6,897

Rep: Reputation: 5018Reputation: 5018Reputation: 5018Reputation: 5018Reputation: 5018Reputation: 5018Reputation: 5018Reputation: 5018Reputation: 5018Reputation: 5018Reputation: 5018
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
 
1 members found this post helpful.
Old 06-29-2017, 02:21 AM   #15
Assomnia
Member
 
Registered: May 2017
Posts: 38

Original Poster
Rep: Reputation: Disabled
Hey all, I'm back

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
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
[SOLVED] Shell Script: "bash: Bad Substitution - Script for remove "." " thiagofw Programming 14 12-09-2016 10:04 PM
[SOLVED] Bash - Capture "Child" script output with "Parent" without using files or coproc keyword Jason_25 Programming 2 02-14-2016 07:51 PM
bash script: using "select" to show multi-word options? (like "option 1"/"o zidane_tribal Programming 7 12-19-2015 01:03 AM
[SOLVED] Can you make "grep" non-case sensitive in Bash script? Regnets1 Programming 4 01-06-2013 01:00 PM
[SOLVED] How to get "case" to overwrite preset variables in a bash script Mogget Programming 4 02-24-2009 01:19 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 08:07 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration