Linux read command

Source: Internet
Author: User

For friends who write bash scripts, the read command is indispensable. You need to practice it to understand the general purpose of the read command:

Write a script:

#! /Bin/bash

# Hao32 test read

Echo-e "Please enter your test :"

Read TESTREAD

Echo $ TESTREAD

Run this script to get a general understanding of the usage of the read command. The following is a detailed usage of the read command.

Function

Read a row from the standard input.

Syntax

Read [-p] [-r] [-s] [-u [n] [VariableName? Prompt]

[VariableName...]

Description

The read command reads a line from the standard input and specifies the value of each field in the input line to the shell variable. The characters in the IFS (internal field separator) variable are used as separators. The VariableName parameter specifies the name of the shell variable. The shell variable obtains the value of a field in the input line. The first shell variable specified by the VariableName parameter specifies the value of each field, the second shell variable specified by the VariableName parameter specifies the value of the second field, and so on until the last field. If the number of fields in the standard input row is greater than the number of shell variables specified by the VariableName parameter, the values of all remaining fields are assigned to the specified final shell variable. If it is less than the number of shell variables, the remaining shell variables are set as empty strings.

Note: If the VariableName parameter is omitted, REPLY is used as the default variable name.

The shell variable set by the read command affects the current shell execution environment.

Flag

-P reads the output of processes running by the Korn shell using the | & (pipeline, & mark name.

Note: The file terminator of the-p Mark causes the process to be cleared, so another process is generated.

-R specifies that the READ command processes a \ (backslash) as a part of the input line without using it as a control character.

-S saves the input as a command in the Korn shell history file.

-U [n] reads a single-digit file descriptor number n as the input. You can use the built-in command ksh exec to open the file descriptor. The default value of n is 0, indicating the keyboard. Value 2 indicates a standard error.

Parameters

VariableName? Prompt specifies the name of a variable and the Prompt to be used. When the Korn shell is interactive, it writes the prompt to a standard error and runs the input. The Prompt contains more than one word and must be enclosed in single or double quotation marks.

VariableName... specifies one or more variable names separated by spaces.

Exit status

This command returns the following export values:

0.

> 0: The file terminator or an error is detected.

Example

The following script prints a file in which the first field in each row is moved to the end of the row.

While read-r xx yy

Do

Print printf "% s/n" $ yy $ xx

Done <InputFile reads a row, divides it into fields, and uses "Please enter:" As the prompt. enter:

Read word1? "Please enter:" The word2 System displays:

Please enter:

You enter:

The value of the hello world variable word1 should be "hello", and the variable word2 should be "world ."

To create a common process, use print-p to write it to the common process, and read the input from the common process with read-p. Enter:

(Read; print "hello $ REPLY ")

Print-p "world"

The line value of the read-p line variable should be "hello world ."

To save the copy of the input line as a command in the history file, enter:

Read-s line <input_file if the input file contains "echo hello world,", "echo hello world" will be saved as a command in the history file.

Welcome to reprint this article, please indicate from: http://www.linuxsense.org

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.