Use of Linux read command

Use of Linux read command

1. Command Introduction

The read command is a built-in command of Shell, which is used to read a single line from the standard input or the file descriptor specified by the -u option, split the read single line into multiple fields according to the IFS variable, and assign the split fields to the specified variable list var_name respectively. The first field is assigned to the first variable var_name1, the second field is assigned to the second variable var_name2, and so on. If the specified variable name is less than the number of fields, the extra fields together with the separator are assigned to the last var_name. If the specified variable command is more than the number of fields, the extra variable assignments are empty. If no var_name is specified, then all the fields after the split are stored in the specific variable REPLY. Of course, it can not only assign variables, but also arrays.

The IFS (Internal Field Separator) variable is a built-in environment variable of the Shell, which is used by the read command to separate a single line into multiple fields. The default value is .

The REPLY variable is also a built-in environment variable of the Shell. When the read command does not specify a receiving variable, it is used to receive a single line of content read by the read command.

2. Command format

read [-ers] [-a aname] [-d delim] [-i text] [-n nchars] [-N nchars] [-p prompt] [-t timeout] [-u fd] [name ...]

3. Option Description

-a [aname]: Store the split fields in the specified array in sequence, starting from the array index 0. -d [delim]: Followed by an identifier, only the first character is useful, used to replace the newline character as the end of the line. -e: You can use the command completion function when entering, and use the Tab key to automatically complete the files in the current directory. -i [text]:If readline is being used to read the line, text is placed into the editing buffer before editing begins
-n [nchars]: followed by a number, defines the length of the input text instead of reading a whole line -N [nchars]: followed by a number, defines the length of the input text instead of reading a whole line. However, if a line is less than nchars characters, the line separator is ignored and the next line is read -p [prompt]: When reading input from the terminal, print a prompt message before the input -r: Shield backslash \. If this option is not available, \ is used as an escape character. If it is, \ is a normal character -s: Quiet mode. When entering characters, they are no longer displayed on the screen, such as entering a password when logging in -t [timeout]: Followed by the number of seconds to define the waiting time for entering characters -u [fd]: Followed by the file descriptor fd, read from the file descriptor

4. Common Examples

(1) If no variable is specified, read will pass the passed value to REPLY, and it can be referenced as long as REPLY is called.

[root@TENCENT64 ~]# read;echo "\$REPLY:$REPLY"
dablelv
$REPLY:dablelv

(2) Read specifies a prompt when reading from the terminal

[root@TENCENT64 ~]# read -p"input u password:";echo "\$REPLY:$REPLY"
input u password:123456
$REPLY:123456

(3) The -t parameter specifies the number of seconds the read command waits for input. When the timer is up, the read command returns a non-zero exit status code.

#!/bin/bash

if read -t 5 -p "Enter website name:" name
then
  echo "The website name you entered is $website"
else
  echo "\nSorry, your input timed out."
fi
exit 0

Execute the program without input and wait for 5 seconds:

Enter website name:
Sorry, your input timed out

(4) In addition to controlling the input time, you can also use the -n option to control the number of characters input. When the number of characters entered reaches the preset number, it automatically exits and assigns the entered data to the variable. For example, exit after receiving only 2 inputs:

#!/bin/bash

read -n2 -p "Please enter two characters: " any
echo "\nThe two characters you entered are: $any"
exit 0

(5) The -s option prevents the input data from being displayed on the command terminal. (In fact, the input content is displayed, but the read command sets the text color to the same color as the background.) This option is often used to enter passwords.

#!/bin/bash

read -s -p "Please enter your password:" pass
echo "\nThe password you entered is $pass"
exit 0

The password is not displayed after the program is executed:

Please enter your password:
The password you entered is runoob

(6) Reading files

Each call to the read command reads a line of text from the file. When there are no more lines to read from the file, the read command will exit with a non-zero status.

while read var1 var2
do
	echo $var1 $var2
done < file.txt

The above is the detailed content of the use of Linux read command. For more information about Linux read command, please pay attention to other related articles on 123WORDPRESS.COM!

You may also be interested in:
  • Linux gzip command compression file implementation principle and code examples
  • gzip command in linux
  • Usage of Linux userdel command
  • Use of Linux date command
  • How to run Linux commands in the background
  • Use of Linux ls command
  • Use of Linux chkconfig command
  • Use of Linux usermod command
  • Use of Linux passwd command
  • Use of Linux ln command
  • Linux cut command explained
  • Use of Linux gzip command

<<:  MySQL InnoDB tablespace encryption example detailed explanation

>>:  IIS7 IIS8 http automatically jumps to HTTPS (port 80 jumps to port 443)

Recommend

Tomcat components illustrate the architectural evolution of a web server

1. Who is tomcat? 2. What can tomcat do? Tomcat i...

Problems with index and FROM_UNIXTIME in mysql

Zero, Background I received a lot of alerts this ...

MySQL 8.0 upgrade experience

Table of contents Preface 1. First completely uni...

SQL merge operation of query results of tables with different columns

To query two different tables, you need to merge ...

Solve the problem of margin merging

1. Merge the margins of sibling elements The effe...

Common functions of MySQL basics

Table of contents 1. Common function classificati...

How to install ElasticSearch on Docker in one article

Table of contents Preface 1. Install Docker 2. In...

Web page custom selection box Select

Everyone may be familiar with the select drop-dow...

Use Javascript to develop sliding-nav navigation plug-in with sliding bar effect

Table of contents 1. Introduction 2. Usage 3. Dev...

A brief discussion on MySQL select optimization solution

Table of contents Examples from real life Slow qu...

Layim in javascript to find friends and groups

Currently, layui officials have not provided the ...