Article From:https://www.cnblogs.com/hb91/p/9969046.html

readCommand – P (prompt statement) – n (number of characters) – t (waiting time) – s (no echo)

1、Basic read
readThe command receives input from standard input (keyboard) or other file descriptors (described later). After the input is obtained, the read command puts the data into a standard variable. The following is the read command.
The simplest form:

1
2
3
4
5
#!/bin/bash
echo -"Enter your name:"   //parameter-nThe function of echo is no newline. By default, echo is newline.
read  name                   //Keyboard input
echo "hello $name,welcome to my program"     //display information
exit 0                       //Quit the shell program.

Because the read command provides the – P parameter, it allows you to specify a prompt directly on the read command line.
So the above script can be abbreviated into the following script:

1
2
3
4
#!/bin/bash
read -"Enter your name:" name
echo "hello $name, welcome to my program"
exit 0

There are only one name or more variables after the read above. At this time, if you input more than one data, the first data is given to the first variable, the second data is given to the second variable, and if you input too many data, all the values are given to the first variable. If too little input does not end。
The read command can also specify no variables on the read command line. If no variables are specified, the read command places the received data in the environment variable REPLY.
For example:
read -p “Enter a number”

The environment variable REPLY contains all the input data and can be used in shell scripts as other variables.

2、Timing input.
There is a potential danger in using the read command. The script is likely to stop and wait for user input. If the input data script must continue to execute, you can specify a timer using the – t option.
-tOption specifies the number of seconds for the read command to wait for input. When the time is full, the read command returns to a non-zero exit state.

1
2
3
4
5
6
7
8
#!/bin/bash
if read -5 -"please enter your name:" name
then
    echo "hello $name ,welcome to my script"
else
    echo "sorry,too slow"
fi
exit 0

In addition to timing the input time, you can also set the read command to count the input characters. When the number of characters entered reaches a predetermined number, it automatically exits and assigns the input data to variables.

1
2
3
4
5
6
7
8
9
10
11
#!/bin/bash
read -n1 -"Do you want to continue [Y/N]?" answer
case $answer in
Y | y)
      echo "fine ,continue";;
N | n)
      echo "ok,good bye";;
*)
     echo "error choice";;
esac
exit 0

 This example uses the – N option followed by a value of 1 to indicate that the read command exits as soon as it accepts a character. Just press the next character to answer, and the read command immediately
Accept input and pass it to variables. No need to press the Enter key.

3、Silent reading (input is not displayed on the monitor)
Sometimes script user input is required, but the input data is not expected to be displayed on the monitor. Typical examples are entering passwords, and of course there are many other data that need to be hidden.
-sOptions enable the data entered in the read command not to be displayed on the monitor (in fact, the data is displayed, except that the read command sets the text color to the same color as the background).

1
2
3
4
#!/bin/bash
read  -s  -"Enter your password:" pass
echo "your password is $pass"
exit 0

4、read file
Finally, you can use the read command to read files on a Linux system.
Each call to the read command reads a line of text in the file. When the file has no readable lines, the read command exits in a non-zero state.
The key to reading a file is how to transfer the data in the text to the read command.
The most common method is to use the cat command on the file and pipe the result directly to the while command containing the read command.
Example::

1
2
3
4
5
6
7
8
9
#!/bin/bash
count=1    //Assignment statement without spaces
cat test | while read line        //cat The output of the command is the input of the read command, and the read read value is placed in the line.
do
   echo "Line $count:$line"
   count=$[ $count + 1 ]          //Note the spaces in parentheses.
done
echo "finish"
exit 0
Link of this Article: Shell read command (transfer)

Leave a Reply

Your email address will not be published. Required fields are marked *