Search the FAQ Archives

3 - A - B - C - D - E - F - G - H - I - J - K - L - M
N - O - P - Q - R - S - T - U - V - W - X - Y - Z
faqs.org - Internet FAQ Archives

Unix - Frequently Asked Questions (2/7) [Frequent posting]
Section - How do I read characters from the terminal in a shell script?

( Part1 - Part2 - Part3 - Part4 - Part5 - Part6 - Part7 - Single Page )
[ Usenet FAQs | Web FAQs | Documents | RFC Index | Cities ]


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?
See reader questions & answers on this topic! - Help others by sharing your knowledge

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 ."

User Contributions:

Comment about this article, ask questions, or add new information about this topic:

CAPTCHA




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 March 27 2014 @ 02:12 PM