Shell Statement Rules

Source: Internet
Author: User
Tags control characters

One, if condition statement

Format:

An IF condition expression

Then #当条件为真时执行以下语句

Command List

else #为假时执行以下语句

Command List

Fi

If statements can also be nested using

If condition expression 1

Then

If condition expression 2

Then

Command List

Else

If condition expression 3

Then

Command List

Else

Command List

Fi

Fi

Else

Command List

Fi

You can do multiple layers of nesting an if statement must be followed by a fi representation that the layer condition ends otherwise it will cause a syntax error

In conjunction with the preceding example, the following are:

Here, let's start with the command test used in a conditional statement to indicate whether the condition after test tests is true

If Test-f "$"

Then

LPR $

Else

If test-d "$"

Then

CD $

LPR $

Else

echo "$ not a file or directory"

Fi

Fi

The above example can also be changed to the following

If Test-f "$"

Then

LPR $

Elif test-d "$" #elif with else if

Then

(CD $1;LPR $)

Else

echo "$ not a file or directory"

The above example does not know whether you understand what is the meaning of it?

If we now save this example as Prfile

chmod +x Prfile

Execute the procedure just now.

./prfile AAA

This example is to check if your input parameter is a file if it is printed if it is a directory first to go to the catalog and then print if

That is, not a file or a directory.

Second, multiple conditions test statement case

Format:

Case string in

mode) command list;;

mode) command list;;

....

Esac

A multi-conditional statement is a case that begins with a Esac end in the middle can have multiple criteria list function is the test string and and inside the pattern has

There is no match, there is the execution inside the command list mode can also be * number denotes any string, each pattern inside the last heart; Double-lead

End, or a syntax error will occur.

While loop

While command format

While condition table

Do

Command table

Done


Sum=0

I=0

while [$i! = "100"]

Do

i= ' expr $i + 1 '

sum= ' expr $Sum + $i '

Done

echo $i $Sum

The operation of this program is to add 1 to 100.


Sum=0

I=0

until [$i = "100"]

Do

i= ' expr $i + 1 '

sum= ' expr $Sum + $i '

Done

echo $i $Sum

When I is not equal to 100, the loop is the loop when the condition is false, otherwise it exits, and the first example is when I is not equal to 100

Loop when the test condition is true.


For loop

Command format:

For variable in name list

Do

Command List

Done

Three. Circular control statements

The break command does not perform the current loop within the body of a break, and the following statement exits from the current loop.

The Continue command is a program that ignores the following statement in this body, starting from the loop header


Shell's embed command

QUOTE:

: null, return forever to True

. To perform an operation from the current shell

Break exits for, while, until, or case statements

CD changes to the current directory

Continue the next step of the execution loop

echo feedback information to standard output

Eval read parameter, execute result command

EXEC executes the command, but not the current shell

Exit exits the current shell

Export the variable so that the current shell can take advantage of it

PWD Displays the current directory

Read reads a line of text from standard input

ReadOnly make variable read-only

Return exit function with return value

Set controls the display of various parameters to the standard output

Shift command line argument to the left offset one

Test evaluation Criteria Expression

Times shows the user and system time of the shell running process

Trap runs the specified command when the signal is captured

Ulimit displaying or setting shell resources

Umask display or set default file creation mode

unset removing variables or functions from shell memory

Wait until the child process finishes running


SED edit command

P Print matching lines

= Show file line number

A \ Appends new text information after locating line numbers

i \ Insert new text information after locating line number

D Delete Anchor row

C \ replace positioned text with new text

s replaces the pattern with the replacement mode

R read text from another file

W write text to a file

Q The first mode match is launched or launched immediately after the completion of the

L Display control characters equivalent to octal A S C I i Code

{} The command group executed on the anchor row

N reads the next line of text from another file and attaches it to the next line

G Paste the pattern 2 into the/pattern n/

Y transfer character

N continues to the next input row; Allows pattern matching statements across rows


SED and regular expressions

S e-D identifies any basic regular expressions and patterns and their row matching rules. One of the rules to remember is that if you want to locate a special character, you must make

Use (\) to mask its special meaning


$ Sed-e '/^#/d '/etc/services | More

This causes the SED to delete any matching rows. Delete lines that begin with #


QUOTE:

Rules

Expression description

/./will match any line that contains at least one character

/.. /will match any line that contains at least two characters

/^#/will match any line starting with ' # '

/^$/will match all empty rows

/}^/will match any line ending with '} ' (no spaces)

/} *^/will match any row that ends with 0 or more spaces followed by '} '

/[abc]/will match any row that contains lowercase ' a ', ' B ', or ' C '

/^[abc]/will match any line starting with ' A ', ' B ' or ' C '


$ sed-n-E '/begin/,/^end/p '/my/test/file | More

If "BEGIN" is not found, then the data will not be printed. If "BEGIN" is found, but not found in all subsequent lines

"END", then all subsequent rows will be printed. This happens because of the flow-oriented nature of the SED--it does not know if "END" will occur.

$ Sed-e ' s/.$//' mydos.txt > Myunix.txt

The script works very simply: an override rule expression matches the last character of a line, and that character happens to be a carriage return. We use NULL characters

Replace it so that it is completely removed from the output. If you use the script and notice that the last character of each line in the output has been deleted,

You have specified a text file that is already in UNIX format. There's no need to do that!


$uniq-u d c-f input-file out-file

QUOTE:

The meaning of its options:

-U displays only non-repeating rows.

-D displays only duplicate rows, with each repeating row showing only one row

-C Prints the number of occurrences of each repeating row.

-F N is a number, and the first n fields are ignored.

Some systems do not recognize the-f option, at which point the substitution is used-N.


In the/L o G S directory, look for files that change time before 5th and delete them:

[ - ]

CODE:

$ find Logs-type f-mtime +5-exec rm {} \;

In order to find and sort all the directories in the current file system, you can use:

[code]$ Find. -type D-print-local-mount |sort

# Find. -type D | Sort

CODE:

(CMD1;CMD2; CMDN) execute a set of commands #在一个子 the shell

{cmd1;cmd2; ...; cmdn}# executes a set of commands in the current shell

This is a basic concept


The original post is published by "Net-human":

ABC: Represents the three consecutive characters of ABC, but is independent of each other rather than set. (Can be simply three char. Set)

(ABC): Represents the collection of three consecutive characters of ABC. (It is simple to think of a char. Set)

A|b: Represents a single character, or a or B.

(ABC|XYZ): represents either ABC or XYZ of these two char. Set. (b)

[ABC]: Represents a single character, which can be a or B or C. (same as Wildcard's [ABC] principle)

[^ABC]: Represents a single character, not a or B or C. (same as Wildcard's [!ABC] principle)


CODE:

^ only matches the beginning of the line

$ only matches end of line

* A single character followed by *, matching 0 or more characters

[] only matches [] inside characters. Can be a single character, or it can be a sequence of characters. can be used-

denotes [] The range of characters within a sequence, as [1-5] instead of [1 2 3 4 5]

\ is used only to mask the special meaning of a meta-character. Because sometimes some metacharacters in S H e l l have

Special meaning. \ can make it lose its meaning

. Match only any single character

p a t t e r n \ {n \} is used only to match the number of occurrences of the preceding p a t T e R N. n is the number of times

P a t e r n \ {n,\} M only has the same meaning, but the minimum number of times is n

p a t t e R n \ {n,m \} only has the same meaning, but p a t T e r n occurs between N and M

2. Match string or character sequence at beginning of line

^ only allow to match characters or words at the beginning of a line.

For example, use the Ls-l command and match the directory.

[ - ]

CODE:

$ ls-l | grep ^d

Example two: Match all empty lines: ^ $

Example three: Return only the line containing one character: ^.$

QUOTE:

Match in regular expression with *. All files ending with p a s:

\ * \ . P a S

The specific meaning of the character * can be masked.

CODE:

A \ {2 \} B

Match value is a a B

CODE:

A \ {2, 4 \} B

The result is a a B, a a A B, a a A a B, not a B or a a A a a B, etc.

The format is as follows: The first 4 characters are numbers, the next is X x, the last 4 are numbers, the operation is as follows:

[0-9] \ {4 \}x x[0-9] \ {4 \}

CODE:

^ to the beginning of the line

$ To Line end

^ [t h e] to start line with t h E

[s] I g n a [l l] pairs of matched words s I g n A l, S i g n A l, S i g n A l, S i g n a l

[Ss]igna[ll]\. Yes, but add a period.

[M a y m a Y] pairs of lines containing M a y uppercase or lowercase letters

^ U S e R $ pair containing only U S e r lines

[tty]$ lines ending with T t y

\ . To a line with a period

^ D. . X. . X. . X a directory that has executable permissions on users, user groups, and other user group members

^ [^ l] List of directories to exclude associated directories

[. * 0] Add any character before or after 0

[0 0 0 *] to 0 0 0 or more

[II] to uppercase or lowercase I

[I i] [n n] to uppercase or lowercase i or n

[^ $] to blank line

[^. * $] to match any string in a row

^ . . . . . . $ pairs including 6-character lines

[A-za-z] for any single character

[A-z] [A-z] * to at least one lowercase letter

[^ 0-9 \ $] For non-digital or dollar identification

[^ 0-0 A-z] to non-numbers or letters

[1 2 3] A number from 1 to 3

[D d] e v i c e pair word d e v i c e or D e v i c E

D E. . C E to the first two letters D E, followed by two arbitrary characters, and finally C E

\ ^ Q to start with ^ q

^ . $ on lines with only one character

^\. [0-9] [0-9] lines starting with a period and two digits

' "D e v i c e" ' pair word d e v i c E

D e [v v] I c e \. The word d e v i c e or d e v i c E

[0-9] \ {2 \}-[0-9] \ {2 \}-[0-9] \ {4 \} pair date format d d-m m-y y y y

[0-9] \ {3 \} \. [0-9] \ {3 \} \. [0-9] \ {3 \} \. [0-9] \ {3 \} to the I P address format nnn. nnn.nnn.nnn

[^. * $] Pairs match any row

Author: Internet time: 2004-11-16 13:

2, line matching

1) Display the text containing the string "4 8":

[ - ]

CODE:

$ grep-c "DATA.F"

4

The G R e p returns the number 4, indicating that there are 4 rows containing the string "4 8".

3) Number of rows

Displays the number of rows that satisfy the matching pattern:

[ - ]

CODE:

$ grep-n "DATA.F"

The number of rows in the output first column, followed by each matching row that contains 4 8.

4) Show non-matching rows

Show all rows that do not contain 4 8

[ - ]

CODE:

$ grep-v "DATA.F"

1. Mode range

To extract a city location with code 4 8 4 and 4 8 3, you can use [] to specify a string range.

[ - ]

CODE:

$ grep "48[34]" data.f

2. Do not match the beginning of the line

To make the beginning of a line not 4 or 8, you can use the ^ mark in square brackets.

[ - ]

CODE:

$ grep "^[^48]" data.f


Shell Statement Rules

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.