Operating System - HP-UX
1832872 Members
2661 Online
110048 Solutions
New Discussion

restricting characters read

 
Praveen Bezawada
Respected Contributor

restricting characters read

Hi
In shell scripting, can we restrict the number of character read ,using the
'read' keyword.
3 REPLIES 3
Gadura Praveen
Frequent Advisor

Re: restricting characters read

Praveen,

As far as I know , read does not have any option to count the number of characters. But you can try something like this :-

read DUMMY
LENGTH=`echo ${DUMMY}|wc -m`
if [ "${LENGTH}" -gt 8 ]; then
ANSWER=`echo ${DUMMY} | cut -c 1-8`
else
ANSWER="${DUMMY}"
fi

And use the value of ANSWER to put the 8 character restriction.

Hope this helps.

Praveen
Carlos Fernandez Riera
Honored Contributor

Re: restricting characters read


WARNING : it is a very hard response

This output is from a menubased aplication:stty -a < /dev/pts/tb
speed 9600 baud; line = 0;
rows = 25; columns = 132
min = 1; time = 0;
intr = ^U; quit = ^\; erase = ^H; kill = ^C
eof = ^D; eol = ^@; eol2 ; swtch
stop = ^S; start = ^Q; susp ; dsusp
werase ; lnext
parenb -parodd cs8 -cstopb hupcl -cread -clocal -loblk -crts
-ignbrk brkint ignpar -parmrk -inpck -istrip -inlcr -igncr -icrnl -iuclc
-ixon -ixany ixoff -imaxbel -rtsxoff -ctsxon -ienqak
-isig -icanon -iexten -xcase -echo echoe echok -echonl -noflsh
-echoctl -echoprt -echoke -flusho -pendin
opost -olcuc -onlcr -ocrnl -onocr -onlret -ofill -ofdel -tostop


____

min=1 ; time = 0

this parameters let keep a only char from keyboard.


See man stty.

AND good luck.
unsupported
James R. Ferguson
Acclaimed Contributor

Re: restricting characters read

Praveen:

You can alter the IFS character and thereby redefine the words read by 'read'.

See "read" and "IFS" in 'man sh-posix'.

...JRF...