Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How to read single character from screen? (unix)

Status
Not open for further replies.

fjweaver

Programmer
Oct 18, 2002
11
0
0
GB
does anyone know if you can read a single character from a screen without hitting the return key and without using the 'read' command.

ie: a read command like below
echo "are you sure you want to do this: 'Y/N' "
read opt
if [ $opt = "Y" ]
continue
else
exit
fi

so instead of doing the the 'read' we just want to press the 'Y' key

thanks
Frank
 
This code should get you started. stty here is for HP-UX and usually differs with other systems.

Code:
#!/bin/sh

echon() {
  if [ -n "`echo -n`" ] ; then
    echo "$@\c"
  else
    echo -n "$@"
  fi
}
getkey() {
  TTYSTATE=`stty -g`
  stty raw
#   stty -echo
  key=`dd if=/dev/tty bs=1 count=1 2>/dev/null`
  stty "$TTYSTATE"
  echo "\nkey is ${key}" | cat -vu
}

echon "Press any key to continue: "
getkey
Cheers,
ND [smile]

bigoldbulldog@hotmail.com
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top