Read command in Linux

Source: Internet
Author: User

1. Basic Reading

The read command receives input from the standard input (keyboard) or other file descriptor (which is said later). After the input is received, the Read command puts the data into a standard variable. Here is the Read command

In the simplest form:

#!/bin/bash

Echo-n "Enter Your Name:"//Parameter-n is not wrapped, echo defaults to line wrapping

Read name//input from keyboard

echo "Hello $name, Welcome to my program"//Display information

Exit 0//Quit Shell program.

//********************************

Because the Read command provides the-p parameter, it allows you to specify a prompt directly on the read command line.

So the script above can be simply written in the following script:

#!/bin/bash

Read-p "Enter Your name:" Name

echo "Hello $name, Welcome to my program"

Exit 0

In the above read after the variable only name one, there can be more than one, if you enter more than one data, then the first data to the first variable, the second data to the second variable, if the number of input data too many, then all the last value to the first variable. If too little input does not end.

//*****************************************

You can also specify no variables on the read command line. If you do not specify a variable, the read command places the received data in the environment variable reply.

For example::

Read-p "Enter A number"

The environment variable reply contains all the input data, and you can use the environment variable reply in your shell script just like you would with other variables.

2, timing input.

There is a potential risk of using the Read command. The script is likely to stop waiting for the user's input. You can use the-t option to specify a timer, whether or not the input data script must continue to execute.

The-t option specifies the number of seconds that the read command waits for input. When the timer is full, the Read command returns a non-0 exit state;

#!/bin/bash

If read-t 5-p "Please enter your name:" Name

Then

echo "Hello $name, Welcome to my Script"

Else

echo "Sorry,too Slow"

Fi

Exit 0

In addition to entering time timings, you can also set the Read command count input character. Automatically exits when the number of characters entered reaches a predetermined number, and assigns the input data to the variable.

#!/bin/bash

Read-n1-p "does want to continue [y/n]?" Answer

Case $answer in

Y | Y

echo "Fine, continue";;

N | N

echo "Ok,good bye";;

*)

echo "Error choice";;

Esac

Exit 0

This example uses the-n option followed by a value of 1, which instructs the read command to exit as soon as it accepts a character. Just press one character to answer and the Read command immediately

Accept the input and pass it to the variable. No need to press ENTER.

3, silent reading (input not displayed on the monitor)

It is sometimes necessary to script user input, but you do not want the input data to appear on the monitor. A typical example is entering a password, and of course there are many other data that need to be hidden.

The-S option enables the data entered in the Read command to not appear on the monitor (in fact, the data is displayed, except that the read command sets the text color to the same color as the background).

#!/bin/bash

Read-s-P "Enter Your password:" Pass

echo "Your password is $pass"

Exit 0

4. Read the file

Finally, you can also use the Read command to read files on a Linux system.

Each call to the Read command reads "one line" of text in the file. When a file does not have a readable row, the read command exits with a non-0 status.

The key to reading a file is how to transfer the data in the text to the Read command.

The most common approach is to use the Cat command for a file and pass the results directly to the while command that contains the read command

Example::

#!/bin/bash

Count=1//assignment statement, without spaces

Cat Test | While the output of the Read line//cat command is input to the Read command, the read value is placed in line

Do

echo "line $count: $line"

count=$[$count + 1]//Note the spaces in brackets.

Done

echo "Finish"

Exit 0


This article is from the Linux Operations sharing blog, so be sure to keep this source http://liangey.blog.51cto.com/9097868/1574985

Read command in Linux

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.