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'