Boston Linux & Unix (BLU) Home | Calendar | Mail Lists | List Archives | Desktop SIG | Hardware Hacking SIG
Wiki | Flickr | PicasaWeb | Video | Maps & Directions | Installfests | Keysignings
Linux Cafe | Meeting Notes | Linux Links | Bling | About BLU

BLU Discuss list archive


[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

input from command line in bash



On Fri, Sep 01, 2006 at 11:34:21AM -0400, Kevin D. Clark wrote:
> 
> Stephen Adler <adler at stephenadler.com> writes:
> 
> > Basically I want to write a shell script which askes,
> > "Are you sure you want to proceed? [N/y] " and then acts according to user
> > input at the command line.


For those of you with cycles to burn (lots of us these days), here is an
expensive  (*resource wise ) way to read one key at a time from the
keyboard.

Frankly - it seems a tad ridiculous. I'm sure theres better ways.

I use in places where I want to minimize the keystrokes needed by the
user to do things.  It allows you to have scripts which are "single
keystroke" navigable.  Naturally this can cause problems too. :-)

(it wouldn't surprise me if readline can be set up to do single char
reads, which would be one better way to do it)


#####################################################################################
#
#       getabyte:  gets a single byte (expensively!) from the tty
#       Syntax:     x=`getabyte`
#
#####################################################################################
function getabyte {
        ttystate=`stty -g`
        stty raw
        stty -echo
        x=`dd if=/dev/tty bs=1 count=1 2>/dev/null`
        stty $ttystate
        stty echo
        echo $x
}
typeset -xf getabyte



     getyN() {
        while   echo -n "${@}"' (y/N) '>&2
        do
                yn=`getabyte`
                case $yn in
                [yY])   return 0                        ;;
                *)      return 1
                esac
        done
     }


     if getyN "Are you your you want to proceed?" ; then
       echo "^M                                             ^Mlaunching missiles now"
     else
       echo "^M                                             ^Mlet's watch a movie instead"
     fi





-- 
Jeff Kinz, Emergent Research, Hudson, MA.
Speech Recognition Technology was used to create this e-mail


-- 
This message has been scanned for viruses and
dangerous content by MailScanner, and is
believed to be clean.





BLU is a member of BostonUserGroups
BLU is a member of BostonUserGroups
We also thank MIT for the use of their facilities.

Valid HTML 4.01! Valid CSS!



Boston Linux & Unix / webmaster@blu.org