Shell - the use of the read command


Basic format used by the read command

The read command can read a line from standard input and split it into fields

The read command reads a single variable :

[wjj@learning ~]$ read name
zhang san
[wjj@learning ~]$ echo $name
zhang san

Because the read command reads the content of a line, spaces will not be used as separators.

The read command can also read multiple variables (variables are separated by spaces) for example :

[wjj@learning ~]$ read num num2 num3
111 222 333 
[wjj@learning ~]$ echo -e "${num}\n${num2}\n${num3}"
111
222
333

In addition, read has a wealth of options that can be added, and its format is: read [addable options] [variable name] , for example: read -a arr.

Options can be added illustrate
-a Assign the obtained data to the array, and the array subscript starts from 0
-d char Set a cut-off character char, not terminated by newlines and spaces, until char is read (not including char)
-e You can use the command completion key
-i text Use text as the initial text of the read line
-n whether Set a number of characters num to read, return after reading num characters, instead of waiting for a newline, but following the delimiter if fewer than NCHARS characters were read before the delimiter
-N whether Set a number of read characters num, return after reading num characters, ignore any delimiter unless encountering EOF or reading timeout (unlike n, N must read enough num characters to terminate)
-p Print the specified string when reading
-r Escape characters are not allowed\
-s Characters are not printed on the terminal as they are entered (the cursor does not move either)
-t time Set an expiration time time, time is in seconds (timeout will not get any characters)
-u fd use file descriptor fd as input source instead of standard input

example

Example of read option

1) -a option

Assign the obtained data to the array, and the array subscript starts from 0.

[wjj@learning ~]$ read -a num
1 2 3 4     
[wjj@learning ~]$ echo -e "${num[0]}\t${num[1]}\t${num[2]}\t${num[3]}"
1	2	3	4

At this point, num is an array, which stores 1 2 3 4; the shell only supports one-dimensional arrays

2) -d option

Set a cut-off character char, not terminated by newlines and spaces, until char is read (not including char)

[wjj@learning ~]$ read -d x name
zhang sanx
[wjj@learning ~]$ echo ${name}
zhang san

read will stop reading when it reaches X, and the read content includes X

3) -e option

You can use the command completion key

[wjj@learning ~]$ read -e file
           
.bash_history  .bash_logout   .bash_profile  .bashrc        study/         text.txt       .viminfo   

You can use the tab key to find files in the current path, or use the tab key to complete the file name.

Use text as the initial text of the read line

4) -n option

Set a number of characters num to read, return after reading num characters, instead of waiting for a newline, but following the delimiter if fewer than NCHARS characters were read before the delimiter.

[wjj@learning ~]$ read -n 10 name
zhangsan
[wjj@learning ~]$ echo $name
zhangsan

It is set to read 10 characters, but 8 characters are read, and the reading is terminated by a newline character.

5) -N option

Set a number of read characters num, return after reading num characters, ignore any delimiter unless encountering EOF or reading timeout (unlike n, N must read enough num characters to terminate)

[wjj@learning ~]$ read -N 10 name
zhangsan
a[wjj@learning ~]$ echo $name
zhangsan a

It is also set to read 10 characters, but the newline character does not terminate the reading, and the newline character is treated as a space until the 10 characters are read.

6) -p option

Print the specified string when read.

[wjj@learning ~]$ read -p "输入用户名" name
输入用户名zhang san
[wjj@learning ~]$ echo $name
zhang san

7) -r option

Escape characters are not allowed during input \and \are only treated as ordinary characters.

[wjj@learning ~]$ read name
\!
[wjj@learning ~]$ echo $name
!
[wjj@learning ~]$ read -r name
\t
[wjj@learning ~]$ echo $name
\t

8) -s option

Characters are not printed on the terminal as they are entered (nor does the cursor move).

[wjj@learning ~]$ read -s password
[wjj@learning ~]$ echo $password
12345

9) -t option

Set an expiration time time, time is in seconds (timeout will not get any characters)

#超时输入的情况,没有及时按enter
[wjj@learning ~]$ read -t 5 phone_num
123456[wjj@learning ~]$ 123456       #read没有读取字符,所以输入的字符跑到命令终端上了
[wjj@learning ~]$ echo $phone_num 

#打印不出来任何东西

#正常输入的情况
[wjj@learning ~]$ read -t 5 phone_num
12345
[wjj@learning ~]$ echo $phone_num 
12345

10) -u option


read reads variables from a file

First, create a file that stores the names of three people.

[wjj@learning ~]$ cat > test << "EOF"
> zhangsan
> lisi
> wangwu
> EOF

1) Put while behind the pipeline to read the file line by line:

#!/bin/bash
count=0    #计数器
cat test | while read name  
do
   let count++
   echo "第${count}位:${name}"
done

2) Only use while for loop reading:

#!/bin/bash
while read name
do
echo ${name} 
done < test

#也可以这样写,赋给多个变量
while read name1 name2 name3  
do
echo ${name1} ${name2} ${name3}
done < test

Guess you like

Origin blog.csdn.net/why1472587/article/details/128511736