Introduction and use of Linuxread commands

Source: Internet
Author: User
I. Overview The read command receives input from standard input (keyboard) or other file descriptors. After the input is obtained, the read command puts the data into a standard variable. II. example (only some common options are listed here) 1. read and copy the code as follows :#!

I. Overview

The read command receives input from standard input (keyboard) or other file descriptors. After the input is obtained, the read command puts the data into a standard variable.

II. example (only some common options are listed here)

1. basic read

Copy codeThe code is as follows:
#! /Bin/bash
Echo-n "Enter your name:" # The function of parameter-n is not to wrap the line, and echo is to wrap the line by default.
Read name # Input from keyboard
Echo "hello $ name, welcome to my program"
Exit 0 # exit the shell program.
It is equivalent to the following:

Copy codeThe code is as follows:
Read-p "Enter your name:" name #-p parameter. you can specify a prompt in the read command line.

 

There is only one or more variables after the read operation. if you enter multiple data, the first data is given to the first variable, and the second data is given to the second variable. If the number of input data is too large, all the remaining values are given to the last variable.

2. you can also choose not to specify variables in the read command line.

If no variable is specified, the read command places the received data in the environment variable REPLY.

Copy codeThe code is as follows:
Read-p "Enter a number"
Echo $ REPLY

 

3. timing input

There is a potential risk of using the read command. The script is likely to stop waiting for user input. If you must continue executing the script regardless of whether the data is entered, you can use the-t option to specify a timer and specify the number of seconds for the read command to wait for the input. When the time is full, the read command returns a non-zero value (0 indicates normal exit );

Copy codeThe code is as follows:
#! /Bin/bash
Ifread-t 5-p "please enter your name:" name
Then
Echo "hello $ name, welcome to my script"
Else
Echo "sorry, too slow"
Fi
Exit 0

 

3. set counting input characters

When the number of characters entered reaches the predefined number, the system automatically exits and assigns the input data to the variable.

Copy codeThe code is as follows:
#! /Bin/bash
Read-n1-p "Do you want to continue [Y/N]? "Answer
Case $ answerin
Y | y)
Echo "fine, continue ";;
N | n)
Echo "OK, good bye ";;
*)
Echo "error choice ";;
Esac
Exit 0
In this example, the-n option is used, followed by the value 1, indicating that the read command will exit as long as it receives one character. As long as the answer is followed by the next character, the read command immediately accepts the input and passes it to the variable. You do not need to press Enter.

 

4. silent reading (the input is not displayed on the monitor)

Sometimes you do not want the input data to be displayed on the monitor. A typical example is to enter a password. Of course there are many other data to be hidden. The-s option does not display the data entered in the read command on the monitor (in fact, the data is displayed, but the read command sets the text color to the same color as the background ).

Copy codeThe code is as follows:
#! /Bin/bash
Read-s-p "Enter your password:" pass
Echo "your password is $ pass"
Exit 0

 

5. read files

Each time you call the read command, the "one line" text in the file is read. When the file has no readable rows, the read command exits in a non-zero state. The key to reading a file is how to transmit data in the text to the read command. The most common method is to use the cat command on the file and directly send the result to the while command containing the read command through the pipeline.

Copy codeThe code is as follows:
#! /Bin/bash
Count = 1
Cat dat | whileread line # The output of the cat command is used as the input of the read command. The read value is placed in line.
Do
Echo "$ count: $ line"
Count = $ ($ count + 1 ))
Done
Exit 0
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.