Bash Shell concludes "II"

Source: Internet
Author: User
Tags case statement logical operators terminates


1. Exit status of the command (1), (Kw:exit status)

Each command has an exit state, represented by a non-negative integer

Status code 0 indicates that the command is running without errors

Non-0 Status code indicates a command error

The range of status codes is 0-255

The exit state of a command is often used for conditional judgment in a process control structure.


2. Conditional expression (2), (Kw:conditional EXPRESSIONS)

compound commands [[, Internal command test and [, use conditional expressions to test the properties and state of a file, do string comparisons and arithmetic comparisons.

form one: [[expression]] supports regular [[Good =~ ^g]]

Form two: [Expression]

Form three: Test expression


Logical operators, high-to-low by priority:

!: Non-

-A: With

-O: Or


Get help information: assist [[, Aid test


3. Process Control (2), (Kw:compound Commands)

If branch structure statement, select the code to run according to the return value of the conditional Judgment command

if COMMANDS; Then

COMMANDS

Elif COMMANDS; Then

COMMANDS

Else

COMMANDS

Fi

Get help If

Case case can only be equal to variable

The case statement is also a branch statement, in the case of long-term judgment can replace if statement, is the code more concise

Case $word in

PATTERN1) COMMANDS;;

PATTERN2) COMMANDS;;

*) COMMANDS;;

Esac

Get help

The while statement is used for the loop structure, and the loop executes as long as the Conditional judgment command returns 0

While COMMANDS

Do

COMMANDS

Done

Getting help:

For the loop structure, each time a loop is taken from the list, a string is removed, and the loop terminates when the string cannot be taken from the list.

For NAME in WORDS

Do

COMMANDS

Done

Get help information for:

For example:

Traverse file name/string

Continue, break

Continue command terminates when cycle, starts next round

Break min Two terminate the entire loop structure

Help continue.


4. Function (2), (kw:functions)

You can think of a function as a small script in a script.

1. Code Reuse

2. Accept Parameters

3. There is a return value

4. Local command sets variables that are used only within the function

5. Return Exit Function

6. Position parameter $ A is the same as the script

6. function library and Source command

7. Export exported function to child shell using


5. Signal (1), (kw:signals)

Signaling is a way of communicating between processes

1. Kill command to signal the process

2. Trap command captures information Sigkill and sigstop cannot be captured, and information that is not captured usually causes the process to terminate


Ctrl-c <--to the foreground process group Hair SIGINT

Ctrl-z <--to the foreground process group Hair SIGTSTP

Ctrl-\ <--to the foreground process group Hair Sigquit

ctrl-d <--output an EOF character


6. Text Processing (4)

1. Common Text Processing commands:

Cat head tail grep sed awk

Cut WC sort Uniq TR TAC Rev

2. Regular expressions are often used in text processing to support more complex, more efficient text processing operations

3. Text Processing Performance Comparison

1. Search

2. Replace

3. Interception


Debug code: Bash-x

Managed code: GIT

Locate-c-M


7. Example Lectures (6)

1. Script Example parsing

2. Script writing

1. command-line parameter handling

2. Run environment Check (permissions, tags, etc.)

3. Specification error code

4. Logging

5. Modular programming (functions, variables)

6. Code alignment and Code style

7. Code comments

8. Code Performance

1. Selection of internal commands and external commands

2. Concurrency, wait, sync (flock)

9. Code debugging (Bash-x, Bash-n)

10. Version management (git, SVN, CVS)

3. Project Demonstration


Write a program that implements the following functions

1. Check that the HTTPD service on the remote server is working correctly.

and record the results of the inspection

2. Provides the ability to add, view, modify, delete

3. Only the root user can write the check record, others may query

4. Avoid multiple processes writing check records at the same time.

If a write conflict occurs,

The corresponding error code should be returned

5. In addition to recording the inspection records, the program should also be run on each

Make a record of every mistake


Bash Shell concludes "II"

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.