Restricting input to only one character

Restricting input to only one character

Post by Sachin Sharm » Tue, 17 Apr 2001 22:07:20



hello,
Can anybody tell me ,if there is any way to restrict the user to enter only
one character as input. i.e. if I do
    read var
it will read a full word.But I want that the user is not allowed to enter
more than
one character.

Also is it possible to accept any input without pressing <Enter>.

with regards
sachin

 
 
 

Restricting input to only one character

Post by Anthony Borl » Tue, 17 Apr 2001 23:45:56



Quote:> hello,
> Can anybody tell me ,if there is any way to restrict the user to enter
only
> one character as input. i.e. if I do
>     read var
> it will read a full word.But I want that the user is not allowed to enter
> more than
> one character.

> Also is it possible to accept any input without pressing <Enter>.

> with regards

Sachin,

Neither of the above tasks can be performed very 'neatly' within a shell
script.

The following script, based on an example in the comp.unix.shell FAQ,
accomplishes both tasks:

#!/bin/sh

echo -n "Enter a character: "
stty cbreak         # or  stty raw
readchar=`dd if=/dev/tty bs=1 count=1 2>/dev/null`
stty -cbreak

echo
echo "You typed in: $readchar"

As you can see, however, it is not a totally 'clean' solution, but it *does*
work.

I hope this helps.

 
 
 

Restricting input to only one character

Post by Roger Denho » Thu, 19 Apr 2001 18:54:56


On Tue, 17 Apr 2001 00:45:56 +1000, "Anthony Borla"




>> hello,
>> Can anybody tell me ,if there is any way to restrict the user to enter
>only
>> one character as input. i.e. if I do
>>     read var
>> it will read a full word.But I want that the user is not allowed to enter
>> more than
>> one character.

>> Also is it possible to accept any input without pressing <Enter>.

Yup.
Using ksh93 the timed read -t option can do this elegantly.
A typeset limits input to one char and can force case and left align
in case of dribble on the space bar.
see www.kornshell.com to get opensource ksh93 from AT&T.
 (thanks guys, much appreciated)

Something like this should do what you want with ksh built ins.
Not tested as I am on a dos box at present. so  RTFM

#!/usr/bin/ksh
typeset -Ll1 input_char

read -t2 input_char
print "you entered $input_char"

cheers
Roger D

 
 
 

Restricting input to only one character

Post by Dan Merc » Fri, 20 Apr 2001 03:14:30




> On Tue, 17 Apr 2001 00:45:56 +1000, "Anthony Borla"



>>> hello,
>>> Can anybody tell me ,if there is any way to restrict the user to enter
>>only
>>> one character as input. i.e. if I do
>>>     read var
>>> it will read a full word.But I want that the user is not allowed to enter
>>> more than
>>> one character.

>>> Also is it possible to accept any input without pressing <Enter>.
> Yup.
> Using ksh93 the timed read -t option can do this elegantly.

I don't think it can.  Read requires a line end delimiter to be received
before it will set the variable:

   $ echo $0
   ksh93
   $ print "c" | read -t1 x
   $ echo $x
   c
   $ print -n "c" | read -t1 x
   $ echo $x

   $ print -n "b:" | read -t1 -d: x
   $ echo $x
   b
   $

So although the read will timeout,  the character will not be read.
With ksh93,  you still need stty and dd.

--
Dan Mercer

- Show quoted text -

Quote:> A typeset limits input to one char and can force case and left align
> in case of dribble on the space bar.
> see www.kornshell.com to get opensource ksh93 from AT&T.
>  (thanks guys, much appreciated)

> Something like this should do what you want with ksh built ins.
> Not tested as I am on a dos box at present. so  RTFM

> #!/usr/bin/ksh
> typeset -Ll1 input_char

> read -t2 input_char
> print "you entered $input_char"

> cheers
> Roger D

Opinions expressed herein are my own and may not represent those of my employer.
 
 
 

1. vi-how do i limit user input to one character?

Does anyone know how I can limit user input to one character? You know, when
the curosr won't move, allowing only one character of input?
If I have
echo -n "Enter A B C D or E"
read input
output=$(expr index "$input" AaBbCcDdEe)
...
I'm having problems in this small script when the user enters more than one of
the above letters...any ideas?
Kim

2. Perl shell for Mac OS X

3. how to split input line to one character per line with just shell

4. problem with Lilo and offboard ide

5. vi-how do i limit user input to one character?

6. Teminal

7. Nawk: can I use more than one input file in one script?

8. IPX

9. Lines selection where the first character equals the last one

10. how to restrict input to awk

11. Restricting User input to only numbers

12. Single character input??