input from command line in bash

Jeff Kinz jkinz at kinz.org
Sat Sep 2 20:36:04 EDT 2006


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.




More information about the Discuss mailing list