shell script based learning

A, shell Introduction

Static language: compiled language

C、C++、JAVA、C#

Dynamic languages: interpreted language

Explain the implementation side edge

PHP、SHELL、python、perl

Process-oriented: Shell,

Variables: memory space, named

Memory: addressing the storage unit

Graphical Interface: Gnome, KDE, Xfce

Command Line Interface: sh, csh, ksh, bash, tcsh, zsh

Shell programming with JavaScript, php programming, as long as one can write code in a text editor and a script interpreter can be interpreted on it

Create a shell script file:

Open a text editor (you can use vi / vim command to create the file), create a new file test.sh, extension sh (sh behalf shell), the extension does not affect the script execution, see the name EENOW like.

Execute a shell script way:

1, ./test.sh (I used to perform in the current working directory, and the file has execute permissions, namely: chmod + x test.sh)

2, by way of an absolute path to execute the script

/test/test.sh

3, directly using bash or sh script to execute

bash test.sh

sh test.sh

Second, the types of variables: length determined in advance and stored in the format data

character

Numerical

Integer

   Float

bash supports quotes:

``: Command substitution

"": Strong references, you can achieve variable substitution

'': Weak references do not complete variable substitution

File name wildcard, globbing

*: Any character of any length

?: Any single character

[]: Matches any single character within a specified range

[Abc], [am], [z], [AZ], [0-9], [a-zA-Z], [0-9a-zA-Z]

[: Space:]: whitespace characters

[: Punct:]: Punctuation

[: Lower:]: lowercase

[: Upper:]: uppercase letters

[: Alpha:]: uppercase and lowercase letters

[: Digit:]: Digital

[: Alnum:]: uppercase and lowercase letters and numbers

Matches any single character outside the specified range: [^]

[[:alpha:]]*[[:space:]]*[^[:alpha:]]

Exercise:;

1, to show all files beginning with a or m;

ls [am]*

2, shows all the file name contains a digital file;

ls * [0-9] * 

ls *[[:digit:]]*

3, show all ends with a number and the file name does not contain blank files;

ls *[^[:space:]]*[0-9]   

4, file name contains special symbols or non-alphabetic file numbers;

ls * [^ [: alnum:]] *

 

三, grep, egrep, fgrep

grep: according to the pattern search text and displays it in line with patterns of lines of text.

Pattern: text characters and regular expression meta-character combination of matching conditions

grep [options] PATTERN [FILE...]

-i

--color

-v: show no pattern is matched to the line

-o: display only mode to the matched string

   -E: Use extended regular expressions

 

 

 

Regular Expressions: 

.: Matches any single character

[]: Matches any single character within a specified range

Matches any single character outside the specified range: [^]

Sets of characters: [: digit:], [: lower:], [: upper:], [: punct:], [: space:], [: alpha:], [: alnum:]

The number of matches (greedy):

*: Matches the preceding character any number of times

a, b, ab, aab, but, adb, amnb

a*b, a\?b

a.*b

*: Any character of any length

\ Front ?: matching character 1 or 0, for example: \ {1 \}

\ {M, n \}: matching characters preceded by at least m times, n times at most. For example: \ {0,3 \}

Anchor position:

^: The anchor of the line, behind any content of this character must appear at the beginning

$: Anchor end of the line, in front of any content of this character must appear end of the line

^ $: Blank Lines

 

\ <Or \ b: anchoring the first word, it must be behind any character as the word's first appearance

\> Or \ b: anchoring suffix, any of its characters must appear in front of the tail as a word

 

 

Exercise:

test.txt contents of the file can be imported by the file system by adding additional redirection (e.g. CAT / etc / >> test.txt the passwd )

 

1, display line test.txt file insensitive to the beginning of s;

grep -i '^s' test.txt

grep '^[sS]' test.txt

2, to the end nologin test.txt display line; 

grep ‘nologin$’ test.txt

3, a # test.txt to beginning, and followed by one or more blank characters, then any connection with any non-blank character row;

grep "^#[[:space:]]\{1,\}[^[:space:]]" test.txt

4, the display test.txt contains: a digital intermediate :( i.e., a digital two colon) line;

grep ':[0-9]:' test.txt

5, the display / line / inittab file begins with a number and to a beginning of the digital number at the end of the same etc;

grep '^\([0-9]\).*\1$' test.txt

Extended regular expressions:

Character matches:

[]

[^]

The number of matches:

*: 

?:

+: Match front character at least once

{m,n}

Anchor position:

^

$

\<

\>

Grouping:

(): Grouping

\1, \2, \3, ...

or

|: or

C | cat: Cat or cat, C or cat

grep -E = egrep 

 

Fourth, the logical operators

Logic: 1 + 1> 2

Logical operations: AND, OR, NOT, XOR

1: True

0: False

1 & 0 = 0

0 & 1 = 0

0 & 0 = 0

1 & 1 = 1

or:

non:

! = True False

! False = true

The logical relationship between the command:

Logical AND: &&

The first condition is false, the second determination condition no longer, the end result has;

The first condition is true, the second condition is determined to be available;

Logical OR: ||

 

 

 

Script: Command of the pile, according to the actual needs, combined with the source command flow control mechanisms implemented

 

Are test scripts for syntax errors:

 

bash -x script: single-step execution. Results as shown:

 

 

#!/bin/bash

# Comment line is not executed

/ Dev / null: software equipment, bit bucket, data black hole

A sub-shell script that starts the process at the time of execution;

The script command line to start would inherit the current shell environment variables;

The system automatically execute the script (non-command line to start) you need to self-define the needs of the environment variables;

 

Five, bash variable type:

Environment Variables

Local variable (local variable)

Position variable

Special Variables

Variable Name:

1, can only contain letters, numbers and underscores, and can not start with a number;

2, should not be with the same name already in the system environment variables;

3, the best-known name to see justice done;

Local variables:

set VARNAME = VALUE: Scope bash for the entire process;

Local variables:

local VARNAME = VALUE: scope of the current code segment;

Environment variables: the scope of the current shell process and its children;

export VARNAME=VALUE

VARNAME=VALUE

export VARNAME "Export"

 

Location variables:

$1, $2, ...

Shift

Special variables:

$ ?: execution state command return value;

Program execution, there may be two types of return values:

Program execution results

Program status return code (0-255)

0: correct execution

1-255: Failure to perform, 1,2,127 reserve system;

$ # Number of parameters

$ * Through all the parameters

$ @ Traverse all parameters

$$ Process id

$ 0 script name

Output redirection:

> Output redirection

>> Additional output redirection

2> error redirection

2 >> append redirection error

&> Full redirector

&>>

<

<< designated termination terminator

 

Sixth, conditional

How to implement conditional bash?

Single-branch if statement

if the determination condition; then

  statement1

  statement2

  ...

be

 

Two-branch if statement:

if the determination condition; then

statement1

statement2

...

else

statement3

statement4

...

be

 

Multi-branch if statement:

determining if the conditions 1; then

  statement1

  ...

elif judgment conditions 2; then

  statement2

  ...

elif determination condition 3; then

  statement3

  ...

else

  statement4

  ...

be

Conditions Test Type:

Integer test

Character test

File test

 

Conditional test expression:

[ expression ]

[[ expression ]]

test expression

If the file is larger than the number of lines test.txt 100, Big displayed;

[ `wc -l test.txt | cut -d' ' -f1` -gt 100 ] && echo "big"

 

Integer comparison:

-eq: Testing two integers are equal; such as $ A -eq $ B

-ne: test whether two integers unequal; range, it is true; equal is false;

-gt: test whether a number is greater than another; greater than is true; otherwise, false;

-lt: test whether a number is less than another; less than true; otherwise, false;

-ge: greater than or equal to

-le: less than or equal to

 

String comparison:

= Equivalent, such as: if [ "$ a" = "$ b"]

== equal, such as: if [ "$ a" == "$ b"], and = equivalent

! = Not equal, such as: if, the operator uses the pattern matching [[]] structure [ "$ a" = "$ b"!].

 

 

 

Exercise, write a script, complete the following requirements:

Given a user:

1, if its UID is 0, displays this as an administrator;

2, otherwise, it displays it as a normal user;

If UID is 0; then

  Shown as Administrator

otherwise

  It appears as a normal user

 

 

Uid.sh:

 

"Reference" implementation of the results of a command, use the command reference; for example: 

a=`wc-l test.txt | cut -d: -f1`;

Use a command execution status results, to directly execute this command, you must not be quoted; 

If you want the results of a command assigned to a variable, use the command references, such as USERID = `id -u user1`;

If you want a result of the execution state saved command and command as the determination condition of success, it is necessary to execute the command, then the result reference in its state, such as

the -u user1

a=$?

This sentence must not be written as a = `id -u user1`;

 

 

Exercise: Write a script

Given a file, such as / etc / inittab

Determine whether there is a blank line in the file;

If so, its number of blank lines are displayed; otherwise, showed no blank lines.

 

 

 

Kongbaihang.sh:

 

 

Seven, shell in arithmetic operations:

A=3

B=6

1, let arithmetic expression

let C=$A+$B

2, $ [arithmetic expression]

C=$[$A+$B]

3, $ ((arithmetic expression))

C=$(($A+$B))

4, expr arithmetic expression, the expression must be space between each operation and number of the operator, but also to use the command references

C=`expr $A + $B`

 

Eight, file test:

-e FILE: Testing whether a file exists

-f FILE: test file is a regular file

-d FILE: test whether the specified path is a directory

-r FILE: Testing whether the current user has read access to the specified file;

[ -e test.txt ]

 

Nine, parameter passing

For example: ./ filetest.sh 3 4

$1: 3

$2: 4

 

Exercise: Write a script

Pass two parameters to the script (integer);

This product of the sum of the two displays, the;

 

Canshu.sh:

 

 

Ten, character test, parameter passing

Character test:

==: Test for equality, equally true, ranging from false

! =: Test if unequal, ranging from true to false, etc.

>

<

-n string: test specified string is empty, it is really empty, empty is not fake

-z string: test whether the specified string is not empty, empty is not true, empty and false

 

Parameter passing:

Exercise: Write a script

Passing a parameter (single character line) to the script, such as the parameters q, Q, quit or the Quit, exit the script; otherwise, the display parameters of the user;

 

Chuancan.sh:

 

 

Eleven, for circulation

Cycle: entry conditions, exit conditions

for variable in list; do

  Loop

done

After traversing the complete exit;

 

How to generate a list:

for i in{ 1..100};do

Loop

done

SEQ `[starting number [step length]]` Number End

`Seq 1 2 12` e.g.

Exercise: write a script, complete the following tasks

1. Create 10 systems account user1-user10

2. Set a password is a random number (characters and numbers)

3. The account number and password generated output to a file

Useradd.sh:

 

Write a script:

Within 100 can calculate all positive integer divisible by 3, and;

 Test.sh:

 

Write a script:

Within 100 calculates all odd and all even and well; the display, respectively;

 

100.sh:

 

XII combination of test conditions

-a: Relationship with

-o: or relationship

!: Non-relational

if [ $# -gt 1 -a $# -le 3 ]

if [ $# -gt 1 ] && [ $# -le 3 ]

 

XIII text processing tools sed

grep, sed ( stream editor ), awk 

sed basic usage:

But: Stream Editor

Line editor  ( full-screen editor : vi)

sed:  the pattern space

The default does not edit the data in the original file, only the pattern space to do processing; then, after the end of treatment, print the pattern space to the screen;

sed [options] 'AddressCommand' file ...

-n:  silent mode, no longer the default content display mode space

-i:  directly modify the original file

-e SCRIPT -e SCRIPT: you can execute multiple scripts simultaneously

-r:  extended regular expressions

For example Below is the original content of the document

 

sed 's / w / a /  g' text.txt not modify the source file

sed -i 's / w / a  / g' text.txt beginning of the second line w is changed to a

 

Some use:

1StartLine,EndLine

For example, 1,100

2/pattern1/,/pattern2/

The first is pattern1 matched to the start line to the first time pattern2 match to the end of the line, all this in the middle of the line

3、LineNumber

Specified row

4、StartLine, +N

From startLine start, backward N rows;

 

Command

d:  Delete the qualifying rows;

p:  show qualifying rows;

a \ string:  the back line specifies additional new line content string

\ n- : it can be used to wrap

 sed  '/^U/a \#hello\n#word' test.txt

 

i \ string:  In front of the line designated to add a new line that reads string

   sed  '/^U/i \#hello\n#word' test.txt

w FILE:  the row address within the specified range of the specified file to save

   sed '/^#/w /root/txt' test.txt

 

s / pattern / string / modifier find and replace, by default only replace each row to the first pattern string is matched

sed 's/#/@/g' test.txt

Plus modifiers

g:  global replace

The text # replaced by @

s///=s###=s@@@

 

sed Exercise:

test.txt content file may be imported by any other system files by appending redirection (e.g. CAT / etc / >> test.txt the passwd )

1 , delete the test.txt file Bank of China's first whitespace;

sed -r 's@^[[:spapce:]]+@@g' test.txt

2 , replacing test.txt file "id: 3: initdefault:" line number is 5 ;

sed 's@\(id:\)[0-9]\(:initdefault:\)@\15\2@g' test.txt

3 , delete the test.txt file blank lines;

sed '/ ^ $ / d' test.txt

4 , delete the beginning of the test.txt file # number

sed 's@^#@@g' test.txt

5 , delete the beginning of a file # number of characters and blank back, but requires # behind the numbers must have blank characters ;

sed -r 's@^#[[:space:]]+@@g' test.txt

6 , delete a file with a blank followed by the # beginning of the line class in the whitespace and #

sed -r 's@^[[:space:]]+#@@g' test.txt

 

 

 

 

 

 

Fourth, a simple script to monitor disk usage

 

Guess you like

Origin www.cnblogs.com/KerwinLai/p/11367551.html