Operating System - Linux
1752676 Members
6422 Online
108789 Solutions
New Discussion юеВ

Re: Keyboard reading from ksh

 
SOLVED
Go to solution
Leo The Cat
Regular Advisor

Keyboard reading from ksh

Hi

How to accept only the ENTER key in a ksh compatible script to provide something like that:
"Please do action ... and press ENTER"

any idea ?

Bests Regards
Den
8 REPLIES 8
Huc_1
Honored Contributor

Re: Keyboard reading from ksh

Please tell us more on what you are trying to do...like where is your script failing?

enjoy, life.

Jean-Pierre Huc

Smile I will feel the difference
Steven E. Protter
Exalted Contributor

Re: Keyboard reading from ksh

Shsalom,


Here is an example. Change the script and it will still work.

#!/bin/bash

filename="/home/paul/myfile.tar.gz"
hostname="ftp.myhost.com"
username="username"
password="password"
ftp -un $hostname <quote USER $username
quote PASS $password

binary
put $filename
quit
EOF

SEP
Steven E Protter
Owner of ISN Corporation
http://isnamerica.com
http://hpuxconsulting.com
Sponsor: http://hpux.ws
Twitter: http://twitter.com/hpuxlinux
Founder http://newdatacloud.com
Ivan Ferreira
Honored Contributor

Re: Keyboard reading from ksh

I'm not sure of what are you trying to do, but normally I use:

echo "Press ENTER to continue"
read
echo "Operation complete"
exit 0
Por que hacerlo dificil si es posible hacerlo facil? - Why do it the hard way, when you can do it the easy way?
James R. Ferguson
Acclaimed Contributor

Re: Keyboard reading from ksh

Hi Den:

If you want to distinguish the action of pressing the ENTER key alone, you could do:

# echo "press ENTER \c";read LINE;[ -z "${LINE}" ] && echo OK || echo "Please just press ENTER"

Regards!

...JRF...
Leo The Cat
Regular Advisor

Re: Keyboard reading from ksh

Hi Guys

In fact, is there another keyboard function. (named readchar by example) I'd like to have only one striked key on input.

ENTER key was not the good example because source of confusion on my question. sorry.

Suppose you have choice between [Y]es or [N]o during execution script. If the user press Y or N it's enough to continue and ENTER is not necessary.

Is it possible ?

Thanks Guys and sorry again about the confusion.

Bests Regards
Den





James R. Ferguson
Acclaimed Contributor
Solution

Re: Keyboard reading from ksh

Hi (again) Den:

Try this:

# cat ./readchar
#!/usr/bin/sh
echo "Enter character: \c"
stty raw
readchar=$(dd if=/dev/tty bs=1 count=1 2 > /dev/null)
stty -raw
echo "\nYou pressed the '${readchar}' key"
exit 0

Regards!

...JRF...
Laurent Menase
Honored Contributor

Re: Keyboard reading from ksh

if you want to be able to get any char

SAVstty=$(stty -g)
stty raw min 1 time 0
a="$(od -A n -c -N 1)
stty $SAVstty
echo $a




if you type a you will get \r
if you type a ^J you will get \n
a backspace \b
a tab \t
.....
if you replace -c in od command by -x then you get the ascii value in hex.

Leo The Cat
Regular Advisor

Re: Keyboard reading from ksh

Thanks Guys last two posts was very helpfull !

The final way to solve this small affair is:

echo -n "Enter a character: " # prompt
/bin/stty -icanon && /bin/stty eol ^A
key=`dd if=/dev/tty bs=1 count=1 2>/dev/null` # read a key press
/bin/stty icanon && /bin/stty eol ^@
echo "$key" # echo newline and/or key -- OPTIONAL


Bests Regards
Den