Home > Article > Operation and Maintenance > What is the use of Linux read command?
In Linux, the read command is used to read keyboard input. It can read a line of data from the standard input and assign the value of each field of the input line to a shell variable. When using redirection , you can read a line of data in the file, the syntax is "read [parameter] [name ...]".
#The operating environment of this tutorial: linux7.3 system, Dell G3 computer.
Linux read command introduction
The read command in Linux reads a line from the standard input and converts each field of the input line Values assigned to shell variables.
Syntax
read [-ers] [-a aname] [-d delim] [-i text] [-n nchars] [-N nchars] [-p prompt] [-t timeout] [-u fd] [name ...]
Parameters
Description | |
---|---|
is followed by a variable, which will be considered as an array, and then assigned a value. By default, spaces are used as delimiters. | |
is followed by an identifier. In fact, only the first character after it is useful as a sign of the end. | |
is followed by a prompt message, that is, the prompt message is printed before input. | |
You can use the command completion function when typing. | |
followed by a number defines the length of the input text, which is very practical. | |
Shield \. If this option is not available, \ will be used as an escape character. If so, \ will be a normal character. | |
Quiet mode, no longer displayed on the screen when entering characters, such as entering a password when logging in. | |
followed by the number of seconds defines the waiting time for entering characters. | |
Followed by fd, read from the file descriptor, which can be newly opened by exec. |
Description
The read internal command is used to read a single line of data from standard input. This command can be used to read keyboard input, and when using redirection, can read a line of data from a file.read command usage example
1. Simple reading
#!/bin/bash echo "请您输入网站名: " #读取从键盘的输入 read website echo "你输入的网站名是 $website" exit 0 #退出
2. Read the file
Every time you call the read command, "one line" of text in the file will be read. When the file has no readable lines, the read command exits with a non-zero status. We can use the cat command to transfer the results directly to the while command containing the read command. The content of the test file test.txt is as follows:AAAAAAAAAAAAAAAAAAAAAAThe script is as follows, you can put the script into xxx.sh for executionBBBBBBBBBBBBBBBBBBBBBB
CCCCCCCCCCCCCCCCCCCCCC
DDDDDDDDDDDDDDDDDDDD
=========== ===== Chuanzhi Podcast
#!/bin/bash count=1 cat test.txt | while read line # cat 命令的输出作为read命令的输入,read读到>的值放在line中 do echo "Line $count:$line" count=$[ $count + 1 ] # 注意中括号中的空格。 done echo "finish" exit 0
3. Example:
read 1987name 从标准输入读取输入并赋值给变量1987name。 read first last 从标准输入读取输入到第一个空格或者回车,将输入的第一个单词放到变量first中,并将该行其他的输入放在变量last中。 read 从标准输入读取一行并赋值给特定变量REPLY。 read -a arrayname 把单词清单读入arrayname的数组里。 read -p "text" 打印提示(text),等待输入,并将输入存储在REPLY中。 read -r line 允许输入包含反斜杠。 read -t 3 指定读取等待时间为3秒。 read -n 2 var 从输入中读取两个字符并存入变量var,不需要按回车读取。 read -d ":" var 用定界符“:”结束输入行。
Add an example of not allowing the password to be displayed when entering the password in the terminal.
方法1: #!/bin/bash read -p "输入密码:" -s pwd echo echo password read, is "$pwd" 方法2: #!/bin/bash stty -echo read -p "输入密码:" pwd stty echo echo echo 输入完毕。Among them, option
-echo prohibits output from being sent to the terminal, while option
echo allows output to be sent.
$ before the variable name. Otherwise, echo will directly output the variable name.
Linux Video Tutorial"
The above is the detailed content of What is the use of Linux read command?. For more information, please follow other related articles on the PHP Chinese website!