Shell -- read command

Source: Internet
Author: User

READ command-N (do not wrap)-P (prompt statement)-N (number of characters)-T (wait time)-S (do not echo)


1. Basic read
The read command receives input from standard input (keyboard) or input from other file descriptors (as described later ). After the input is obtained, the READ command puts the data into a standard variable. The following is the READ command
The simplest form ::

#! /Bin/bashecho-n "enter your name:" // The parameter-N does not apply to line breaks. Echo uses line breaks by default. Read name // input echo "Hello $ name, welcome to my program "// display information exit 0 // exit the shell program.

Because the READ command provides the-p parameter, you can specify a prompt directly in the READ command line.
Therefore, the preceding script can be abbreviated as the following script ::

#!/bin/bashread -p "Enter your name:" nameecho "hello $name, welcome to my program"exit 0

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 values are given to the first variable. If there are too few inputs, it will not end.
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.
Example ::
Read-P "enter a number"

The environment variable reply contains all input data. You can use the environment variable reply in a shell script just like other variables.

2. Timing input.
There is a potential risk of using the READ command. The script is likely to stop waiting for user input. If the script must be executed no matter whether the data is entered, you can use the-T option to specify a timer.
-T option specifies the number of seconds for the READ command to wait for input. When the time is full, the READ command returns a non-zero exit status;

#!/bin/bashif read -t 5 -p "please enter your name:" namethen    echo "hello $name ,welcome to my script"else    echo "sorry,too slow"fiexit 0

In addition to time input, you can also set the READ command to count the input characters. When the number of characters entered reaches the predefined number, the system automatically exits and assigns the input data to the variable.

#!/bin/bashread -n1 -p "Do you want to continue [Y/N]?" answercase $answer inY | y)      echo "fine ,continue";;N | n)      echo "ok,good bye";;*)     echo "error choice";;esacexit 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
Accept the input and pass it to the variable. You do not need to press Enter.


3. Silent Reading (the input is not displayed on the monitor)
Sometimes script user input is required, but the input data is not expected 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 ).

#!/bin/bashread  -s  -p "Enter your password:" passecho "your password is $pass"exit 0

4. Read files
Finally, you can use the READ command to read files in Linux.
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 transmit the result to the while command containing the READ command through the pipeline.
Example ::

#! /Bin/bashcount = 1 // value assignment statement without spaces CAT test | while read line // the output of the cat command is used as the input of the read command, put the read value in the line do echo "line $ count: $ line" Count = $ [$ count + 1] // note the spaces in the brackets. Doneecho "finish" Exit 0

# I personally think this article provides a good explanation of the read command. The content of this article comes from the network.

Shell -- read command

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.