Solved

Determine if F1 key is pressed in Bash script.

Posted on 2004-09-15
8
2,838 Views
Last Modified: 2012-05-05
Hi, I'm trying to determine if a Function key (F1)  has been pressed.

Using - dumpkeys - I know the key code for F1 is "\033[[A"

Now, I write a bash script like this

#!/bin/sh
echo please enter the F1 function key
read -sn3 key
if [ $key == "\033[[A" ]; then
        echo you pressed the F1 key
        else
        echo you pressed some other key
fi

But this dosen't work. Can someone tell me how to do this proper.
Thanks


0
Comment
Question by:Airgazm
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 3
8 Comments
 
LVL 23

Expert Comment

by:Mysidia
ID: 12069840
well ^[[[A is 4 characters.

But if the user presses something else it will only be one character.
Perhaps

read -sn1 key
if [ $key == "\033" ] ; then
     read -sn3 key
     if [ $key == "[[A" ] ; then
          echo "You pressed the F1 key"
     else
          echo "You pressed some other key"
     fi
else
  echo "You pressed some other key"
fi
0
 

Author Comment

by:Airgazm
ID: 12069923
Did you try that on your system? It dosen't work correctly here.

I'm pretty sure a function key is actually 3 keys but I could be wrong.  

If I do a simple command on prompt

read key
when I press the F1 key it returns ^[[A  - 3 chars plus the esc

I have looked on the net for an answer and have found nothing covering how Linux returns these special codes with a read function.



0
 
LVL 11

Accepted Solution

by:
dimitry earned 250 total points
ID: 12070242
This code is working:
--------------------------------------------------------------------------------
#!/bin/sh
f1='\[\[A'
f2='\[\[B'
SUCCESS=0
echo please enter the F1 function key
read -n4 key                      # Read 4 characters.
echo -n "$key" | grep "$f1"  #Check if character code detected.
if [ "$?" -eq $SUCCESS ]
then
  echo "F1 key pressed."
  exit $SUCCESS
fi
echo -n "$key" | grep "$f2"  #Check if character code detected.
if [ "$?" -eq $SUCCESS ]
then
  echo "F2 key pressed."
  exit $SUCCESS
fi
0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

Author Comment

by:Airgazm
ID: 12070415
Hmm, that code dosen't work correctly either.  

It didn't print anything other than the char input "^[[A" and then a ~







0
 

Author Comment

by:Airgazm
ID: 12070422
Opps I meant char input "^[[11~"  

0
 
LVL 11

Expert Comment

by:dimitry
ID: 12070430
It is working on my RH 9.0 machine...
Are you sure you took the code as is ?
0
 

Author Comment

by:Airgazm
ID: 12070597
Ok, you were right, I was using Putty and it wasn't set to Linux mode, I changed it and now it works.  
Seems like the code could be more simple but it does what it is suppose to do.

Thanks
0
 
LVL 11

Expert Comment

by:dimitry
ID: 12070817
You are welcome
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

The purpose of this article is to fix the unknown display problem in Linux Mint operating system. After installing the OS if you see Display monitor is not recognized then we can install "MESA" utilities to fix this problem or we can install additio…
The purpose of this article is to demonstrate how we can upgrade Python from version 2.7.6 to Python 2.7.10 on the Linux Mint operating system. I am using an Oracle Virtual Box where I have installed Linux Mint operating system version 17.2. Once yo…
Michael from AdRem Software explains how to view the most utilized and worst performing nodes in your network, by accessing the Top Charts view in NetCrunch network monitor (https://www.adremsoft.com/). Top Charts is a view in which you can set seve…
Sometimes it takes a new vantage point, apart from our everyday security practices, to truly see our Active Directory (AD) vulnerabilities. We get used to implementing the same techniques and checking the same areas for a breach. This pattern can re…

628 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question