Shell Study-shell Scripting Common sense (these are often used, but a variety of materials on the network are vague things, personally think more useful)

Source: Internet
Author: User
Tags alphabetic character character classes character set control characters manual echo command numeric regular expression

Shell Online Chinese manual abs,shell Chinese course, Shell Chinese course-submarine Eagle (tank) Blog

http://manual.51yip.com/shell/


Http://mprc.pku.edu.cn/mentors/training/TrainingCourses/material/ShellProgramming.HTM

Common sense of shell script programming

Seven Types of files

Regular expressions

Character class description

Shell's quotation mark type

Different modes when setting a variable:

Condition test

Command execution order

Script debugging

Some commonly used small trick

Print some header information

Create an empty file with a length of 0

Some of the commonly used shell variables

Use of $ A

The use of shift

Specify the number of rows to be consulted with the head or tail instruction

awk usage Rules

First awk

Multiple fields

External scripts

BEGIN and END blocks

Regular expressions and blocks

Conditional statements

numeric variables

A string of variables

Numerous operators

Field delimiter

Number of fields

Record number

Multiple rows of records

OFS and ORS

Change multiline to tab-delimited format

Loop structure

For loop

Break and Continue

Array subscript string

Array Tools

Formatted output

String functions

Some of the more intriguing functions

String substitution

Special String form

SED usage rules

sed example

Another SED example

Address range

Address with regular expression

More about the address

Replace

Rule expression Confusion

More character matches

Advanced Replacement Functionality

Combined use

Multiple commands for an address

Attach, insert, and change rows

Several examples of using SED

Common Linux Scripts and functions

Linux Common commands

Instructions for file/directory processing:

Instructions for process processing:

Instructions for string processing:

Instructions for online queries:

Network Application Instructions:

VI Common Skills


Common sense of shell script programming

(These are often used, but all kinds of materials on the web are vague, personally think more useful) seven types of files

D Directory L Symbolic link

s socket file B block device file

C-character device file p named pipe file

-Regular file regular expressions

When you extract or filter text from a file or command output. Regular Expressions (RE) can be used, and regular expressions are a collection of special or not very special string patterns.

Basic meta-Character set:

^ matches only the beginning of the line.

$ matches only the end of the line.

* A single character immediately following *, matching 0 or more characters.

[] matches the characters in [], either as a single character or as a sequence of characters. Can make

Use-to denote a [] inner range, such as [1-5] equivalent to [1,2,3,4,5].

\ masks The special meaning of a meta-character, such as \$ represents the character $, and does not represent a matching row

Tail.

. matches any single character.

Pattern\{n\} matches the number of occurrences of pattern n

Pattern\{n,\}m matches the number of occurrences of pattern, but the minimum number of occurrences is n

Pattern\{n,m\} matches pattern occurrences between N and M (N,m is 0-255)

A few common examples:

Show executable files: ls–l | grep ... x...x. X

Show folders only: Ls–l | grep ^d

Match all blank lines: ^$

Match all the words: [A-Z a-z]*

Match any non-alphabetic character: [^a-z A-z]

Line with eight characters: ^........$ (8). Character class Description

The following is a fairly complete list of the available character classes:

[: Alnum:] alpha-numeric [A-Z 0-9]

[: Alpha:] letter [A-z]

[: Blank:] Space or TAB key

[: Cntrl:] any control character

[:d Igit:] number [0-9]

[: Graph:] Any visible character (no spaces)

[: Lower:] lowercase [A-z]

[:p rint:] Non-control characters

[:p UNCT:] punctuation character

[: Space:] Space

[: Upper:] uppercase [A-z]

[: xdigit:] hex digit [0-9 a-f a-f]

It is advantageous to use character classes as much as possible, as they can be better adapted to non-English locales (including some required accent characters, etc.). Shell's quotation mark type

There are four types of reference in the shell:

"" Double quotation marks

' Single quotation mark

' Anti-quote

\ Backslash

L "" can refer to any character or string other than $, ', \, or, and the variable in "" will display the value of the variable normally.

The difference between "and" "is that the shell ignores any reference value.

Example: Girl= ' GIRL '

echo "The ' $GIRL ' did well '

Then print: The ' girl ' did well

L ' is used to set the output of a system command to a variable, and the shell will use the contents of "as a system command and perform the quality."

For example, echo ' date ' prints the current system time.

L \ used to block special meanings of characters:& * + ^ $ ' "| ?

For example: Expr 12 \* 12 will output different modes when the 144 variable is set:

Valiable_name=value setting the actual value into Variable_name

Valiable_name+value if Variable_name is set, reset its value

Valiable_name:?value If Variable_name is not set, the undefined user error message is displayed first

Valiable_name?value If Variable_name is not set, a system error message is displayed

Valiable_name:=value If Variable_name is not set, its value is set

Valiable_name-value, but the value is not set to the variable_name condition test

The test command is used for testing strings, file states, and numbers, and expr tests and executes numeric outputs.

Test format: Test condition or [condition] (it is important to note that there is a space on both sides of the condition, otherwise it will be an error), the test command returns 0 to indicate success.

The three tests of test are described below, respectively:

N File status test (Common)

-D test whether the folder

-F Tests whether generic files

-l test whether to link files

-R test File is readable

-W test file is writable

-X test whether the file is executable

-S test file is not empty

N String Test

Five formats: Test "string"

Test String_operator "String"

Test "string" String_operator "string"

[String_operator "string"]

["String" String_operator "string"]

Where string_operator can be: = two strings are equal

! = Two String unequal

-Z Empty string

-N Non-empty string

N Numerical Test

Two formats: "Number" Number_operator "number"

["Number" number_operator "number"]

Where: Number_operator can be:-eq,-ne,-gt,-lt,-ge

Example: number=130

["990" le "995" –a "number"-GT "133"]

(Where-a means "and" before and after results)

The L expr command is generally used for integer values, but can also be used for strings.

N format: Expr srgument operator operator argument

For example: Expr 10 + 10

Expr 10 ^ 2 (10 squared)

Expr $value + 10

N Increment count ――expr the most basic usage in the loop

Example: Loop=0

loop= ' expr $LOOP + 1 '

n Pattern matching: Computes the number of characters in a string by the specified colon option

Example: Value=account.doc

Expr $value: ' \ (. *\). Doc '

Output Account command execution order

&& executes a command successfully before executing the next

|| One command execution fails before another command is executed

() executes a set of commands in the current shell (format: (command 1; command 2; ...) )

{} same ()

Example: Comet Mouth_end | | (echo "Hello" | mail dave; exit)

If there is no (), the shell executes the last command (exit) script debugging directly

The most useful tool for debugging scripts is the echo command, which allows you to print information about variables or actions at any time to help locate errors. You can also use the print last state ($?) command to determine if the command is successful, so be aware that you want to output $ now after executing the command you want to test, otherwise $? will change.

The set command can also be used to assist with script testing:

Set–n Read command But do not execute

Set–v Show all rows that are read

Set–x Show all commands and their parameters

(To turn off the SET option, just put-switch to + on it, here a bit special, pay attention to)

some commonly used small trick Print some header information

Command << Dilimiter

......

......

Dilimiter

Use the contents of the demarcation symbol Dilimiter as the standard input for the command

Often used in the echo command, this avoids the need to use an echo command without outputting a line, and the adjustment of the output format becomes simpler.

Example: Echo << something_message

************************************************

Hello, welcome to use my shell script

************************************************

Something_message

The output will be on the screen:

************************************************

Hello, welcome to use my shell script

************************************************

One, the use of << decomposition symbol nature can also automatically select the menu or to implement automatic FTP transmission

This means that the menu is automatically selected using the properties of the decomposition symbol.

For example:./menu_choose >>output_file 2>&1 <<choose

2

3

Y

Choose

Automatically takes a step-by-step decision to execute the script: 2,3,y

<< This nature determines that it is an ideal tool for accessing a database, and it can be used to enter various choices that are made when facing a database hint. create an empty file with a length of 0

Execute the > file_name command or Touch file_name command. Some of the commonly used shell variables

$# the number of arguments passed to the script

$* displays all parameters passed to the script in a single string (can be greater than 9)

ID number of the current process that the $$ script is running

$! ID number of the last process running in the background

$@ with $ #相同, but use quotation marks and return each parameter in quotation marks

$-shows the current options used by the shell

$? Shows the exit status of the last command, 0 means no error (this variable is also often used to print out the output, when the script debugging when a shell command or a function is executed correctly, but note that $ is the most recent function or command exit status, so printing should be printed immediately to obtain the correct information) use of $ A

There is a positional variable $n in the variable that holds the arguments passed in when the function call or script executes, where $ $ represents the function name or script name, and it is important to note that the script name at this time passes the script name that contains the full path. From $1-$9, the first-to-nineth argument passed in, such that the parameter cannot be more than nine, and if more than nine, it can be read using the shift instruction that will be mentioned below.

Because we store the function name or the script name, we can use echo $ to output the call information, but since we are storing the full pathname, we can get the script name with a shell command, basename $ $ will get the part of the name in $ A, and in contrast, dirname $ 0 will get the part of the path in $ A. the use of shift Specify the number of rows to be consulted with the head or tail instruction

Example: The first 20 lines of lookup file: head–20 file_name

10 lines after file lookup: tail–10 file_name

awk Usage Rules

Awk is a great language. Awk is suitable for text processing and report generation, and it has a number of well-designed features that allow for special skills to be programmed. Unlike some languages, awk's syntax is more common. It draws on some of the best parts of some languages, such as C, Python, and bash (although technically, Awk was created earlier than Python and bash). Awk is the kind of language that will become a major part of your strategic coding library once you learn it.

First awk

Let's go ahead and start using awk to understand how it works. On the command line, enter the following command:

$ Awk ' {print} '/etc/passwd

You will see the contents of the/etc/passwd file appear in front of you. Now, explain what awk has done. When awk is called, we specify/ETC/PASSWD as the input file. When you execute awk, it executes the print command in turn for each row in/etc/passwd. All outputs are sent to stdout, and the results are identical to the execution catting/etc/passwd.

Now, explain the {print} code block. In awk, curly braces are used to group several pieces of code together, which is similar to the C language. There is only one print command in the code block. In awk, if only the Print command appears, the entire contents of the current line are printed.

Here is another example of awk, which works exactly like the previous example:

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.