LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (http://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   BASH --> esc key in a case statement (http://www.linuxquestions.org/questions/linux-newbie-8/bash-esc-key-in-a-case-statement-759927/)

manwithaplan 10-05-2009 08:58 PM

BASH --> esc key in a case statement
 
This is just an example... I would like it when I press "esc" "^]" to exit and run my leavin function. But it only is printing the esc charcter on the screen. Still a novice and I can't find info on the ABS guide.

Code:

echo -e "This script is used to copy all the PKGBUILDS needed into a build
directory of choice\n
This portion of print is a test"

read key
 case $key in
        *) abscopy ;;
      255) leavin ;;
 esac


Its been awhile since I have been scripting... Though I can get the escape character to work with a dialog box...

lutusp 10-05-2009 09:07 PM

Quote:

Originally Posted by manwithaplan (Post 3708942)
This is just an example... I would like it when I press "esc" "^]" to exit and run my leavin function. But it only is printing the esc charcter on the screen. Still a novice and I can't find info on the ABS guide.

Code:

echo -e "This script is used to copy all the PKGBUILDS needed into a build
directory of choice\n
This portion of print is a test"

read key
 case $key in
        *) abscopy ;;
      255) leavin ;;
 esac


Its been awhile since I have been scripting... Though I can get the escape character to work with a dialog box...

In order to read single characters, you would have to jump through hoops, and afterward you terminal session will be totally messed up.

Try to design your script so that it doesn't require responses to single keystrokes.

manwithaplan 10-05-2009 09:13 PM

Quote:

Originally Posted by lutusp (Post 3708956)
In order to read single characters, you would have to jump through hoops, and afterward you terminal session will be totally messed up.

Try to design your script so that it doesn't require responses to single keystrokes.

Thanks ... After spending more then hour on different escape sequences, your advice is sound... I adjusted my read with a -s switch and just going to go with a Ctrl-C.

Though if I could write a function that responded to characters on the keyboard would be nice... I might all together abandon this bash and use python.

michaelk 10-05-2009 10:04 PM

Its not to messy.

Code:

read -s -n1  key

 case $key in
    $'\e') echo "Escape";;
 esac


manwithaplan 10-05-2009 10:18 PM

Quote:

Originally Posted by michaelk (Post 3709004)
Its not to messy.

Code:

read -s -n1  key

 case $key in
    $'\e') echo "Escape";;
 esac


Ya I have tried this ... for some reason it always defaults to my "abscopy" function

Code:

clear
echo -e "This script is used to copy all the PKGBUILDS needed into a build
directory of your coice\n
Press Enter to continue or Ctr-C to quit"
read -s -n1 key
case $key in
        *) abscopy ;;
    $'\e') exit ;;  # I've specified my "leavin" function and even exit.
esac

So when I press the esc key it always continues the program with my "abscopy" function, even though its not specified in the case.

slakmagik 10-05-2009 10:23 PM

You need your 'default' case at the end. Otherwise '*' will match before anything else is reached.

manwithaplan 10-05-2009 10:33 PM

Quote:

Originally Posted by slakmagik (Post 3709022)
You need your 'default' case at the end. Otherwise '*' will match before anything else is reached.

DOPE :banghead:.... its been awhile ... forgot that there was a specific order...


Thanks


All times are GMT -5. The time now is 06:23 PM.