Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Determine if F1 key is pressed in Bash script.

Posted on 2004-09-15
8
Medium Priority
?
2,877 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 1000 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
Industry Leaders: 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

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

Question has a verified solution.

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

Have you ever been frustrated by having to click seven times in order to retrieve a small bit of information from the web, always the same seven clicks, scrolling down and down until you reach your target? When you know the benefits of the command l…
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…
Video by: ITPro.TV
In this episode Don builds upon the troubleshooting techniques by demonstrating how to properly monitor a vSphere deployment to detect problems before they occur. He begins the show using tools found within the vSphere suite as ends the show demonst…
Visualize your data even better in Access queries. Given a date and a value, this lesson shows how to compare that value with the previous value, calculate the difference, and display a circle if the value is the same, an up triangle if it increased…

715 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