Read on the Linux receiving keyboard and read on the linux keyboard

Source: Internet
Author: User

Read on the Linux receiving keyboard and read on the linux keyboard



What program does Linux use to process keyboard input?

Linux processes the file/dev/tty on the keyboard!

/Dev/tty this is the device description file of the keyboard and display. Writing to this file is equivalent to displaying it on the user's screen, and reading is equivalent to obtaining user input from the keyboard.

Even if the input and output of a program you write are "<" or ">" redirected, the program can still exchange data with the terminal through this file.

The following is the sample code:

FILE * file_tty;
Int c;

File_tty = fopen ("/dev/tty", "r ");
If (file_tty = NULL)
Exit (1 );

/* Read from tty. If ctrl + d is used, it is EOF. Do not execute the following Program */
If (c = getc (file_tty ))! = EOF)
{
...
}

In bash programming in linux, read does not receive a carriage return when receiving user input, but only displays ^ M. How can this problem be solved?

Use the-d option to set the Terminator, for example:
Read-d "#" var
In this case, # indicates that the user input is complete.
However, to display the carriage return as ^ M, you need to perform additional replacement:
Echo $ var | sed's // ^ M/G'

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.