Home > Operation and Maintenance > Linux Operation and Maintenance > What is the use of Linux read command?

What is the use of Linux read command?

青灯夜游
Release: 2023-02-15 10:39:17
Original
2495 people have browsed it

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

What is the use of Linux read command?

#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 ...]
Copy after login

Parameters

##ParametersDescription-a is followed by a variable, which will be considered as an array, and then assigned a value. By default, spaces are used as delimiters. -d is followed by an identifier. In fact, only the first character after it is useful as a sign of the end. -p is followed by a prompt message, that is, the prompt message is printed before input. -eYou can use the command completion function when typing. -n followed by a number defines the length of the input text, which is very practical. -rShield \. If this option is not available, \ will be used as an escape character. If so, \ will be a normal character. -sQuiet mode, no longer displayed on the screen when entering characters, such as entering a password when logging in. -t followed by the number of seconds defines the waiting time for entering characters. -uFollowed 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  #退出
Copy after login

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:

AAAAAAAAAAAAAAAAAAAAAA

BBBBBBBBBBBBBBBBBBBBBB
CCCCCCCCCCCCCCCCCCCCCC
DDDDDDDDDDDDDDDDDDDD
=========== ===== Chuanzhi Podcast

The script is as follows, you can put the script into xxx.sh for execution

#!/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
Copy after login

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
用定界符“:”结束输入行。
Copy after login

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 输入完毕。
Copy after login
Among them, option

-echo prohibits output from being sent to the terminal, while option echo allows output to be sent.

Note: When using the echo command to output variable values, you must add the symbol

$ before the variable name. Otherwise, echo will directly output the variable name.

Related recommendations: "

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!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template