[ Usenet FAQs | Web FAQs | Documents | RFC Index ]
    Search the FAQ Archives

Part1 - Part2 - Part3 - Part4 - Part5 - Part6 - Part7 - Single Page

Top Document: Unix - Frequently Asked Questions (2/7) [Frequent posting]
Previous Document: How do I get the current directory into my prompt?
Next Document: How do I rename "*.foo" to "*.bar", or change file names to lowercase?


How do I read characters from the terminal in a shell script?



2.5)  How do I read characters from the terminal in a shell script?

      In sh, use read.  It is most common to use a loop like

            while read line
            do
                    ...
            done

      In csh, use $< like this:
        
            while ( 1 )
                set line = "$<"
                if ( "$line" == "" ) break
                ...
            end

      Unfortunately csh has no way of distinguishing between a blank
      line and an end-of-file.

      If you're using sh and want to read a *single* character from the
      terminal, you can try something like

            echo -n "Enter a character: "
            stty cbreak         # or  stty raw
            readchar=`dd if=/dev/tty bs=1 count=1 2>/dev/null`
            stty -cbreak

            echo "Thank you for typing a $readchar ."



Top Document: Unix - Frequently Asked Questions (2/7) [Frequent posting]
Previous Document: How do I get the current directory into my prompt?
Next Document: How do I rename "*.foo" to "*.bar", or change file names to lowercase?

Part1 - Part2 - Part3 - Part4 - Part5 - Part6 - Part7 - Single Page


[ Usenet FAQs | Web FAQs | Documents | RFC Index ]

Send corrections/additions to the FAQ Maintainer:
tmatimar@isgtec.com (Ted Timar)

Last Update October 22 2009 @ 05:35 AM

Some parts © 2009 Advameg, Inc.