HPE GreenLake Administration
- Community Home
- >
- Servers and Operating Systems
- >
- Operating Systems
- >
- Operating System - HP-UX
- >
- Reading single keypress
Operating System - HP-UX
1829575
Members
2734
Online
109992
Solutions
Forums
Categories
Company
Local Language
back
Forums
Discussions
Forums
- Data Protection and Retention
- Entry Storage Systems
- Legacy
- Midrange and Enterprise Storage
- Storage Networking
- HPE Nimble Storage
Discussions
Forums
Discussions
Discussions
Discussions
Forums
Discussions
back
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
- BladeSystem Infrastructure and Application Solutions
- Appliance Servers
- Alpha Servers
- BackOffice Products
- Internet Products
- HPE 9000 and HPE e3000 Servers
- Networking
- Netservers
- Secure OS Software for Linux
- Server Management (Insight Manager 7)
- Windows Server 2003
- Operating System - Tru64 Unix
- ProLiant Deployment and Provisioning
- Linux-Based Community / Regional
- Microsoft System Center Integration
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Blogs
Information
Community
Resources
Community Language
Language
Forums
Blogs
Go to solution
Topic Options
- Subscribe to RSS Feed
- Mark Topic as New
- Mark Topic as Read
- Float this Topic for Current User
- Bookmark
- Subscribe
- Printer Friendly Page
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
04-16-2002 10:29 AM
04-16-2002 10:29 AM
Hello Guys,
I need a way to read a single keypress from a script. For example, is there an easy way to determinine if the user has pressed 'Y' or 'N' while not requiring the Carriage Return to be pressed? I tried various tricks with the stty command but nothing seems to work. Please help.
Thank you, Mary
I need a way to read a single keypress from a script. For example, is there an easy way to determinine if the user has pressed 'Y' or 'N' while not requiring the Carriage Return to be pressed? I tried various tricks with the stty command but nothing seems to work. Please help.
Thank you, Mary
Solved! Go to Solution.
2 REPLIES 2
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
04-16-2002 10:39 AM
04-16-2002 10:39 AM
Solution
Hi Mary:
There is no simple way to do this from the shell but there is a small C program I've used for a very long time. It does an ioctl to disable the line-buffered input mode. If the status of the command is 0 then your character is output to stdout. This indicates that the character was in the printable range ' ' thru '~'; if the character was outside the range ' ' thru '~' then the exit status is the character. It then restores the keyboard for normal input. Note: This program does not recognize multi-character sequences; you will get only the first character of such a sequence.
Use it like this:
echo "Enter a character: \c"
CH=$(onechar)
echo
STAT=$?
if [ ${STAT} -eq 0 ]
then
echo "You pressed \"${CH}\""
else
echo "Your character was not printable"
echo "ASCII Value = ${STAT}"
fi
Compile the attached code like this:
cc onechar.c -o onechar
It's K & R C so it will compile with the bundled C compiler.
Regards, Clay
There is no simple way to do this from the shell but there is a small C program I've used for a very long time. It does an ioctl to disable the line-buffered input mode. If the status of the command is 0 then your character is output to stdout. This indicates that the character was in the printable range ' ' thru '~'; if the character was outside the range ' ' thru '~' then the exit status is the character. It then restores the keyboard for normal input. Note: This program does not recognize multi-character sequences; you will get only the first character of such a sequence.
Use it like this:
echo "Enter a character: \c"
CH=$(onechar)
echo
STAT=$?
if [ ${STAT} -eq 0 ]
then
echo "You pressed \"${CH}\""
else
echo "Your character was not printable"
echo "ASCII Value = ${STAT}"
fi
Compile the attached code like this:
cc onechar.c -o onechar
It's K & R C so it will compile with the bundled C compiler.
Regards, Clay
If it ain't broke, I can fix that.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
04-16-2002 10:46 AM
04-16-2002 10:46 AM
Re: Reading single keypress
Hi,
Here is a way to do it. Not very elegant
perhaps, but it works:
#!/bin/sh
echo -n "Enter a character: \c"
stty raw
WHOAMI=$(who am i)
TTY=$(echo $WHOAMI | cut -f 2 -d " ")
readchar=`dd if=/dev/$TTY bs=1 count=1 2>/dev/null`
stty -raw
echo ""
echo "Thank you for typing a $readchar ."
Regards
Olav
The opinions expressed above are the personal opinions of the authors, not of Hewlett Packard Enterprise. By using this site, you accept the Terms of Use and Rules of Participation.
Company
Events and news
Customer resources
© Copyright 2025 Hewlett Packard Enterprise Development LP