Path: utzoo!utgpu!jarvis.csri.toronto.edu!mailrus!csd4.milw.wisc.edu!lll-winken!uunet!mcvax!hp4nl!philapd!phigate!philmds!leo From: leo@philmds.UUCP (Leo de Wit) Newsgroups: comp.unix.questions Subject: Re: SYSV version of \"grabchars.c\". Keywords: shell script GNU head stty Message-ID: <1026@philmds.UUCP> Date: 10 May 89 11:33:33 GMT References: <19462@adm.BRL.MIL> <1201@aplcen.apl.jhu.edu> Reply-To: leo@philmds.UUCP (Leo de Wit) Organization: Philips I&E DTS Eindhoven Lines: 39 In article <1201@aplcen.apl.jhu.edu> bink@aplcen.apl.jhu.edu (Greg Ubben) writes: |In article <19462@adm.BRL.MIL> mark@ria-emh2.army.mil (Mark D. McKamey) writes: |> I have recently recieved a copy of Mr. Dan Smith's "grabchars" program |> which allows you to get one or more keystrokes from the user, without |> requiring them to hit RETURN within a shell script. The problem I have is |> that the program was written for BSD UNIX. Has anyone ported said program |> to SYSV UNIX? If so, would you please send me the SYSV patches. Thank You. | | I wrote a System V script to allow single-keystroke input too, and came |up with a more general solution (not requiring the "grabchars" program). |I first used the stty command to allow reading single character responses. |Something like "stty -icanon min '^a'" but I don't have AFSVM handy, so |this is probably wrong. Be sure to use stty -g to save the current modes |in a variable first, and set up a trap to restore them. Then the only way I |found to actually read one character without a newline was to use a "head" |program I wrote (before I gnu about knew). The code looked something like: | | case `head -1c` in | [Yy]) | ... | [Nn]) | ... | |I would expect that GNU head could be used the same way. Multi-sequence |keys would fool it (such as function keys) -- does grabchars handle this? |Is there a better way yet (in shell)? This one will not handle multi-sequence keys, but is probably more portable (you don't need a -c flag to head): First setup the terminal to allow single character reads (like you did, or somthing like 'stty raw' or 'stty cbreak' in a BSD environment). Then dd bs=1 count=1 if=/dev/tty 2>/dev/null will read a single character from the keyboard (and yes, restore the tty settings afterward). Leo.