Skip site navigation (1)Skip section navigation (2)
Date:      Mon, 22 Jun 2009 14:57:42 -0400 (EDT)
From:      vogelke+unix@pobox.com (Karl Vogel)
To:        freebsd-questions@freebsd.org
Subject:   Re: Changing my login directory
Message-ID:  <20090622185742.E8677BEEB@kev.msw.wpafb.af.mil>
In-Reply-To: <20090622091220.GA79438@ei.bzerk.org> (message from Ruben de Groot on Mon, 22 Jun 2009 11:12:20 %2B0200)

next in thread | previous in thread | raw e-mail | index | archive | help
>> On Mon, 22 Jun 2009 11:12:20 +0200, 
>> Ruben de Groot <mail25@bzerk.org> said:

R> No need for any external programs. The following shell script [accepts
R> one key, no need to hit return]

R> % more readkey.sh
R>  echo -n "Press any key: "
R>  stty cbreak -echo
R>  KEY=$(dd bs=1 count=1 2>/dev/null)
R>  stty -cbreak echo
R>  echo
R>  echo "You pressed the \"$KEY\" key."

   Advantages of grabchars:

   * One program invocation, instead of running stty twice and dd once.

   * NOT having to deal with vendor-specific stty crap on different Unixen,
     which is a definite bite in the nether regions when trying to make
     things work on Solaris and BSD.

   * Erase/kill processing handled for you, so you can still do ^X or ^U
     deletion if you're entering multiple characters.

   * Handles mapping characters to upper/lowercase, timeouts, default
     answers if return is hit, and limiting answers to given character
     sets.  For example, "grabchars -c 0123456789 -n2 -t10" get two numbers
     with a 10-second timeout.

-- 
Karl Vogel                      I don't speak for the USAF or my company
Never do anything against conscience even if the state demands it.
                                                         --Albert Einstein



Want to link to this message? Use this URL: <https://mail-archive.FreeBSD.org/cgi/mid.cgi?20090622185742.E8677BEEB>