Use of the Read command in Linux-(6/30)

Source: Internet
Author: User
Tags readable

The read command is a very important bash command for entering text from the keyboard or table and interacting with the user, which can read the values of multiple variables at once, and the variables and input values need to be separated by spaces. After the read command, if you do not specify a variable name, the read data will be automatically assigned to a specific variable reply,read parameters less use of the more than a few parameters include:-A (for arrays),-p (give input prompt),-t (Specify the time to read the value of the unit is seconds),- S (does not display input values, generally used for password input); Of course, read can also not use parameters.

Read can be available with-a,-D,-E,-N,-p,-r,-T, and-s eight options.

-A: Read content into numeric values

Echo-n "Input muliple values into an array:"
Read-a Array
echo "Get ${#array [@]} values in array"

-D: Represents the delimiter, the delimiter, which is normally the interval of the IFS parameter, but through-D, we can define the character position that reads until execution occurs. For example Read–d Madfds value, when reading a character with M, does not continue to read backwards, for example, the input is Hello m, the valid value is "Hello", please note that the space in front of the m will be deleted. You can enter multiple strings in this way, such as define "." As a knot symbol and so on.

-e: Only for scripts that interact with each other, and it uses ReadLine to collect input rows. Read these few words don't understand what meaning, first skip.

-N: Used to limit the maximum number of characters that can be read in as valid. For example Echo–n 4 value1 value2, if we try to enter 12 34, then only the previous valid 12 3, as input, in fact, after you enter the 4th character ' 3 ', automatically end the input. The result here is value 12,value2 of 3.

-P: For the prompt, in the previous example we used the Echo–n "..." to give a prompt, you can use the Read–p ' ... my promt? ' The way value is represented by just one statement.

-r: In the parameter input, we can use the '/' to indicate that there is no input, newline continues to enter, if we need to line the last '/' as a valid character, can be done through-R. Also in the input characters, we want special characters such as/n to take effect, and the-r option should also be used.

-S: For some special symbols, such as arrows, do not print them on the terminal, such as Read–s key, we press the cursor, after the carriage return, if we ask to display, that is, ECHO, the cursor upward, if not using-s, at the input, the input display ^[[a, That is, print on the terminal, and then if ECHO is required, the cursor will move up.

-T: Used to indicate the time to wait for input, in seconds, to wait longer, to continue execution of subsequent scripts, note that the parameter retains its original value as NULL input

2. Read related examples

A. Stitching files

#将afile文件中的前三行与bfile中的前四行拼接在一起
While Read-u3 i && read-u4 j;do
echo $i $j
Done 3<afile 4<bfile

B. The input is not displayed on the terminal

Read-p "Input passwd:"-S passwd
Echo $Passwd

C. Time-limited input, otherwise exit

#延迟五秒, no input will automatically exit
Read-p "Input A number:"-T 5 number

D. read qualified characters

#从输入中取5个字符
Read-p "Input A word:"-N 5 Word

E. Wait for output Q to exit

#输入, until you enter Q, it will automatically exit
Read-dp-p "Input Some words end with Q:" Word


#####################################
Http://blog.sina.com.cn/s/blog_4077692e0100r7ct.html

Read command-N (no newline)-p (hint statement)-N (number of characters)-T (wait time)-s (not echoed)

1. Basic Reading

The read command receives input from the standard input (keyboard) or other file descriptor (which is said later). After the input is received, the Read command puts the data into a standard variable. Here is the Read command

In the simplest form:

#!/bin/bash

Echo-n "Enter Your Name:"//Parameter-n is not wrapped, echo defaults to line wrapping

Read name//input from keyboard

echo "Hello $name, Welcome to my program"//Display information

Exit 0//Quit 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 script above can be simply written in the following script:

#!/bin/bash

Read-p "Enter Your name:" Name

echo "Hello $name, Welcome to my program"

Exit 0

In the above read after the variable only name one, there can be more than one, if you enter more than one data, then the first data to the first variable, the second data to the second variable, if the number of input data too many, then all the last value to the first variable. If too little input does not end.

//*****************************************

You can also specify no variables on the read command line. If you do not specify a variable, 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 you can use the environment variable reply in your shell script just like you would with other variables.

2, timing input.

There is a potential risk of using the Read command. The script is likely to stop waiting for the user's input. You can use the-t option to specify a timer, whether or not the input data script must continue to execute.

The-t option specifies the number of seconds that the read command waits for input. When the timer is full, the Read command returns a non-0 exit state;

#!/bin/bash

If read-t 5-p "Please enter your name:" Name

Then

echo "Hello $name, Welcome to my Script"

Else

echo "Sorry,too Slow"

Fi

Exit 0

In addition to entering time timings, you can also set the Read command count input character. Automatically exits when the number of characters entered reaches a predetermined number, and assigns the input data to the variable.

#!/bin/bash

Read-n1-p "does 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, which instructs the read command to exit as soon as it accepts a character. Just press one character to answer and the Read command immediately

Accept the input and pass it to the variable. No need to press ENTER.

3, silent reading (input not displayed on the monitor)

It is sometimes necessary to script user input, but you do not want the input data to appear on the monitor. A typical example is entering a password, and of course there are many other data that need to be hidden.

The-S option enables the data entered in the Read command to not appear 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).

#!/bin/bash

Read-s-P "Enter Your password:" Pass

echo "Your password is $pass"

Exit 0

4. Read the file

Finally, you can also use the Read command to read files on a Linux system.

Each call to the Read command reads "one line" of text in the file. When a file does not have a readable row, the read command exits with a non-0 status.

The key to reading a file is how to transfer the data in the text to the Read command.

The most common approach is to use the Cat command for a file and pass the results directly to the while command that contains the read command

Example::

#!/bin/bash

Count=1//assignment statement, without spaces

Cat Test | While the output of the Read line//cat command is input to the Read command, the read value is placed in line

Do

echo "line $count: $line"

count=$[$count + 1]//Note the spaces in brackets.

Done

echo "Finish"

Exit 0

The read command receives input from the standard input (keyboard) or other file descriptor (which is said later). After the input is received, the Read command puts the data into a standard variable. Here is the Read command

In the simplest form:

#!/bin/bash

Echo-n "Enter Your Name:"//Parameter-n is not wrapped, echo defaults to line wrapping

Read name//input from keyboard

echo "Hello $name, Welcome to my program"//Display information

Exit 0//Quit 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 script above can be simply written in the following script:

#!/bin/bash

Read-p "Enter Your name:" Name

echo "Hello $name, Welcome to my program"

Exit 0

In the above read after the variable only name one, there can be more than one, if you enter more than one data, then the first data to the first variable, the second data to the second variable, if the number of input data too many, then all the last value to the first variable. If too little input does not end.

//*****************************************

You can also specify no variables on the read command line. If you do not specify a variable, 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 you can use the environment variable reply in your shell script just like you would with other variables.

2, timing input.

There is a potential risk of using the Read command. The script is likely to stop waiting for the user's input. You can use the-t option to specify a timer, whether or not the input data script must continue to execute.

The-t option specifies the number of seconds that the read command waits for input. When the timer is full, the Read command returns a non-0 exit state;

#!/bin/bash

If read-t 5-p "Please enter your name:" Name

Then

echo "Hello $name, Welcome to my Script"

Else

echo "Sorry,too Slow"

Fi

Exit 0

In addition to entering time timings, you can also set the Read command count input character. Automatically exits when the number of characters entered reaches a predetermined number, and assigns the input data to the variable.

#!/bin/bash

Read-n1-p "does 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, which instructs the read command to exit as soon as it accepts a character. Just press one character to answer and the Read command immediately

Accept the input and pass it to the variable. No need to press ENTER.

3, silent reading (input not displayed on the monitor)

It is sometimes necessary to script user input, but you do not want the input data to appear on the monitor. A typical example is entering a password, and of course there are many other data that need to be hidden.

The-S option enables the data entered in the Read command to not appear 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).

#!/bin/bash

Read-s-P "Enter Your password:" Pass

echo "Your password is $pass"

Exit 0

4. Read the file

Finally, you can also use the Read command to read files on a Linux system.

Each call to the Read command reads "one line" of text in the file. When a file does not have a readable row, the read command exits with a non-0 status.

The key to reading a file is how to transfer the data in the text to the Read command.

The most common approach is to use the Cat command for a file and pass the results directly to the while command that contains the read command

Example::

#!/bin/bash

Count=1//assignment statement, without spaces

Cat Test | While the output of the Read line//cat command is input to the Read command, the read value is placed in line

Do

echo "line $count: $line"

count=$[$count + 1]//Note the spaces in brackets.

Done

echo "Finish"

Exit 0

1. Do not use parameters

[email protected] bash]# read Namechen[[email protected] bash]# echo $namechen

2. Read multiple variables, separate the variables with spaces, and enter the values to correspond to the spaces.

[[email protected] bash]# read a B C1 2 3[[email protected] bash]# echo "a= $a b= $b c= $c" A=1 b=2 c=3

3.-aparameter , using the-a parameter is equivalent to defining an array variable, enter if it is a number of values you need to use a space separated, use the array must not forget the "{}" curly braces.

[[email protected] bash]# read-a fruitapple banana orange[[email protected] bash]# echo "All fruit is ${fruit[0]} ${fruit [1]} ${fruit[2]} "all fruit apple banana orange

4.-p parameter , give the input prompt, give the specified information at the time of input, and assign the input information to Var.

[Email protected] bash]# Read-p "Enter everything:" Varenter everything:new[[email protected] bash]# echo $varnew

5.-s parameter , does not display input information, usually used for password input

Read-p "Enter your password:"-S Pwdenter your password:[[email protected] bash]# echo $pwd 123

6.-t parameter, specify the time to wait for the user to enter, and end the input if no information is entered within the specified time

[Email protected] bash]# read-t newabc[[email protected] bash]# echo $NEWABC

7.-R parameter , which allows the input value to contain a backslash "\" and a backslash as a value output

[Email protected] bash]# read-r abc\a[[email protected] bash]# echo $abc \a

8.-D parameter , with the specified character as the end input of the command, before the specified terminator is entered, the input window always exists pressing the ENTER key is also useless, the following example is the use of semicolons as the end of the input or other matching can be used.

[Email protected] bash]# read-d ";" bb11221111; [Email protected] bash]# echo $BB 1122 1111

9.-n parameter , reads the specified character to the variable, and the command terminates automatically when you finish typing the specified character.

[Email protected] bash]# read-n 3 nameaaa[[email protected] bash]# echo $nameaaa

Sake I specified 3 input characters to the name variable, and when I finished entering the third A, the command was automatically terminated.

Use of the Read command in Linux-(6/30)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.