Linux Bash Shell (19): String I/O -- read

Source: Internet
Author: User

This article is the seventh chapter of "Learning the bash Shell" 3rd Edition, "Input/Output and Command-Line Processing. Our goal is to read books thin.

Echo and printf are both written. read is used for reading. The format is:

ReadVar1 var2...

Read multiple parameter values using IFS. If we provide multiple values and the number of parameters, all the extra values will be assigned to the last commitment letter. If you do not want to assign values to subsequent values, you can directly use the carriage return to terminate the read operation. In shell, read can use the output after pipe as the input, assign values to a parameter, and process files row by row. Generally, it is written as follows:

While (read a line) do

Process the line

Print the processed line

End

 

Read files row by row

If read does not read any, the exit status is 1 (not 0 ). File can be input through redirection. For example, we use the command test to process the file. For more information, see Linux Bash Shell (17th): I/O redirection.

, You can use:

  • ./Test <file
  • Cat file |./test

Suppose there are two parameters in each row of file. We want to read them row by row and assign values to the x and y variables for the two parameters in row by row.

Function wei_test0

{

Echo "Test 'read '"

While read x y; do

Echo "x = $ x y = $ y"

# Done <file allows I/O redirection.

Done
#}< File can also be implemented through I/O redirection after the function.

}

# Please note that this I/O redirection method is not passed through $ *, $1, $ @. We can use $ # In the function to check for 0, these methods are used for parameter input without redirection. <File: Uses file as the standard input. The following describes the subshell redirection method.

Wei_test0 <file

Cat file | wei_test0

In the above example, in the script file, we use two different redirection methods to test them separately. Redirection not only targets commands, but also subshell, function, and statements in the script. The priority of redirection is the highest priority for the statements closest to read. In addition to the three methods, you can also use command block. Command block is a combination of multiple statements. Use {} to enclose these statements in the same way as C. Reusable {... ...} <File method.

The example above can be more complex. In the file, add # to the front of the line to indicate the comment. During reading, we need to ignore these annotations.

Function wei_test0

{

Echo "Test 'read' $ #"

While read x y; do

If [-z $ (echo $ x | grep '^ #')]; then

Echo "x = $ x y = $ y"

Fi

Done

}

For how to use grep, see Linux Bash Shell (14): command line options

.

User input

Below is one of the most common methods to give a prompt and then read the parameter:

Echo-n 'input you value here :'

Read my_value

If we want the prompt not to be displayed in strout, but in stderr, we can use echo-n '.... '> & 2, returns the detailed file descriptor 2, that is, stderr.

Read options

Read can have eight options:-a,-d,-e,-n,-p,-r,-t, and-s.

-

: Read the content into the value

Echo-n "Input muliple values into an array :"

Read-a array

Echo "get $ {# array [@]} values in array"

-D

: Delimiter, that is, the delimiter. Generally, it is based on the interval between IFS parameters. However, through-d, we can define the position where the execution occurs all the time. For example, read-d madfds value does not continue to read backward when reading m characters. For example, if the input is hello m and the valid value is "hello ", note that spaces in front of m will be deleted. In this way, you can enter multiple strings, such as defining "." As a knot symbol.

-E

: Used only for interactive scripts. readline is used to collect input rows. Skip this sentence if you do not understand what it means.

-N

: Specifies the maximum number of characters that can be effectively read. For example, echo-n 4 value1 value2. If we try to input 12 34, only the first valid 12 3 is used as the input. In fact, after you enter 4th characters '3, the input is automatically ended. Here the result is 12 for value and 3 for value2.

-P

: Used to give a prompt. In the previous example, we used echo-n "… "To give a prompt, you can use read-p '... My promt? 'Value' is represented by only one statement.

-R

: In parameter input, we can use '/' to indicate that the input is not complete, and the line feed continues. If we need the '/' At the end of the line as a valid character, you can use-r. In addition, the-r option should also be used for special characters such as/n to take effect.

-S

: For some special symbols, such as arrows, do not print them on terminal, such as read-s key, we press the cursor, after the carriage return, if we want to display, that is, echo. The cursor is up. If-s is not used, ^ [A, that is, print on terminal, is displayed in the input field. If echo is required, the cursor is moved.

-T

: Indicates the waiting time. The unit is seconds. If the waiting time exceeds, the script is executed. Note that the parameter is not null and the original value is retained.

Related Links: My articles on Linux operations

Related Article

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.