Shell learns 32 days----read reads a row

Source: Internet
Author: User

Standard input and output with standard error output

Standard input/The output is probably the most basic concept in the design principles of software tools..His idea was to:The program should have a data source,Data Export(where are the data going?),and where the problem is reported..they're called standard inputs, respectively .,standard output and standard error output.The program should not know or care about its input and output behind is another program executed!programs can expect,at the time he started,,These standard locations are already open,and ready to use..

By default , the program reads the standard input , writes the standard output , and passes the error message to the standard error output . Such programs we call filters. , because they filter the data stream , Each one performs some sort of operation on the data stream. , and then through the pipeline , Pass it to the next .

To read a row using read

The Read command is an internal command for reading input from a terminal or a file , and the Read command reads an entire line of input , and the newline character at the end of each line is not read into . in the Read back , If you do not specify a variable name , the data being read is automatically assigned to a specific variable REPLY.

Syntax :

Read [-r] Variable

Purpose : reads information into one or more shell variables

Main options :

-r: Raw Read , no processing . do not interpret backslashes at the end of a line as a continuation character .

Behavior Pattern :

Read lines from standard input(Data)after,throughShellfunction of field cutting(Use$IFS)to Slice.The first word is assigned to the first variable,the second word is assigned to the second variable,by the second analogy.If the word is more than the variable,then all the remaining words,full assignment to the last variable. ReadOnce the end of the file is encountered,will exit with a failure.

If the input line ends with a backslash , read discards the backslash and newline character , and then continues to read the next row of data . If you use - R Options , so Read the last backslash is read literally .

Warning :

When you read When applied in a pipeline many shell any with read The variable set by will not keep them in the parent shell loop in the middle of the pipeline

Case One :

Bash code :

#!/bin/bash

Read-p "Input Numbers"

Echo $REPLY

The result of the execution is : input Numbers $REPLY ( The number you entered )

Case two :

#!/bin/bash

Both ()

{

Read-p "Input 2 Numbers" V1 v2

echo $ (($v 1+ $v 2))

}

Both

Execution :./read1.sh

Output result : Input 2 numbers 5 6

11

Case Three :

#!/bin/bash

Read-n 1-p "Do you want to continue [y/n]?" Answer

Case $answer in

Y|y)

echo "Continue"

;;

N|n)

echo "Break"

;;

*)

echo "Error"

;;

Esac

Exit 0

Analysis : This example uses the- n option , which means that the input of how many characters can be accepted at a later time , specifying 1 means to exit by accepting a character , which means that the input is accepted and passed to the variable as soon as a key is pressed . no need to press the carriage return .

Case Four :

#!/bin/bash

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

Then

echo "Hello $name, Welcome to My World"

Else

echo "Sorry, too slow"

Fi

Exit 0

Analysis:It 's used here.- TOptions,UseReadThere is a potential danger to the command..the script is likely to stop waiting for the user's input.if the script must continue regardless of whether the input data is executed,then you can use- Toption to specify a timer.-Toption specifiesReadnumber of seconds the command waits for input.when the count reaches- Twhen the time of execution, readcommand returns a non-0 exit status.-Tthe number of seconds after the option is specified.

Case Five :

#!/bin/bashread-s-P "Enter Your password:" Passecho "Your password is $pass" Exit 0

Analysis : The s option enables the data entered in the read command to not be displayed on the monitor (in fact, the data is displayed, just read command to set the text color to the same color as the background).

Case Six :

How do I get a string with only IP ?

/sbin/ifconfig eth0 | grep bcast | Sed-e ' s/^.* addr:\ (. *\) bcast.*$/\1/'

If you want to implement an IP control with an IP on the machine , see if it exists .

#!/bin/bash

ip=$ (/sbin/ifconfig eth0 | grep bcast | sed-e ' s/^.* addr:\ (. *\) bcast.*$/\1/')

Read Var

#echo $ip

If ["$var" = "$ip"]

Then

echo "OK"

Else

echo "No"

Fi

Analysis : reviewing the sed command , the sed command is an online editor that processes a single line of content at a time . Sed of the command - e the option is to say multi-point editing , here is equivalent to :

Ifconfig eth0 |grep "inet" | Sed ' s/^.*addr://g ' | Sed ' s/bcast.*$//g '

Sed reference connections :

http://blog.csdn.net/dawnstar_hoo/article/details/4043887

References to special symbols :

Http://www.ahlinux.com/shell/9964.html

Shell learns 32 days----read reads a row

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.