Read options and usage in Shell

Source: Internet
Author: User

1. 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 arrayecho "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, in seconds. If the waiting time exceeds, the script is executed. Note that the parameter will keep the original value if it is not null.

2. Read-related instances

A. Splice files

# Splice the first three lines in the afile file with the first four lines in the bfile while read-U3 I & read-U4 J; doecho $ I $ jdone 3 <afile 4 <bfile

B. The input is not displayed on the terminal.

read -p "Input passwd:" -s Passwdecho $Passwd

C. Enter the value at a time; otherwise, exit.

# If there is no input, the system will automatically exit read-P "input a number:"-T 5 Number

D. Read qualified characters

# Read-P "input a word:"-N 5 word

E. Wait for the output Q to exit

# Input until Q is entered, read-DP-P "input some words end with Q:" Word

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.