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 important 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 inputs and outputs there is a running program behind it!program can anticipate,at the time he started,,These standard locations are already open,and is ready to use the.

By default , the program reads the standard input , writes the standard output , and passes the error message to the standard error output . This program we call Filters . , as they filter the data stream , Each will run 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 file , and the Read command reads the entire line of input , and the newline character at the end of each line is not read into . in the Read back , assuming no variable name is specified , The data being read will be assigned to a specific variable on its own initiative. REPLY.

Syntax :

Read [-r] Variable

Purpose : reads information into one or more shell variables

Main options :

-r: Raw Read , no matter what 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 segmentation(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.assuming more words than variables,then all the rest of the words,full assignment to the last variable. ReadOnce the end of the file is encountered,will exit with a failure.

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

Warning :

When you willReadwhen applied to a pipe,manyShellruns it within a separate process..in such a case,no matter what theReadThe variable that is set,will not keep them in the father'sShellthe value in.loops in the middle of the pipe,that's it ..

Case One :

Bash code :

#!/bin/bash

Read-p "Input Numbers"

Echo $REPLY

The result 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

Run :./read1.sh

Output result : Input 2 numbers 5 6

11

Case Three :

#!/bin/bash

Read-n 1-p want to continue [y/n]?

"Answer

Case $answer in

Y|y)

echo "Continue"

;;

N|n)

echo "Break"

;;

*)

echo "Error"

;;

Esac

Exit 0

Analysis : The sample uses the- n option , which means that the input of how many characters can be accepted in the back , specifying the 1 means to exit with a single character , that is, simply pressing a key will immediately accept the input and pass it to the variable . 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.assume that the input data scripts must continue to run regardless of whether they are,then you can use- Toption to specify a timer.-Toption specifiesReadnumber of seconds the command waits for input.when the count reaches- Twhen the run time, 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 allows the data entered in the read command to not be displayed on the monitor (in fact, the data is displayed, only the read command to set the text color to the same color as the background).

Case Six :

How do I get a string that just has an IP ?

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

You want to implement an IP comparison with an IP on the machine to 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 : recall 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 connection :

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.