Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

Shell scripts and regular expressions

2025-02-22 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Network Security >

Share

Shulou(Shulou.com)06/01 Report--

Writing shell scripts and regular expressions:

1. The basics of shell script:

1. First of all, what exactly is the shell script?

1) plain text document-the data stored in the file is stored in characters

2) the combination of a large number of commands to solve user problems according to the needs of users.

3) "execution idempotency"-the result of multiple execution of any command is consistent.

Note: many commands do not have "execution idempotence". In shell scripts, a large amount of program logic is needed to determine whether a command meets its running conditions in order to avoid serious errors.

two。 The basic code content of the script:

1) first, we can use the text editing tool to write shell scripts:

For example: Nano, vi, Vim, emacs, pico

We recommend using Vim here because it has syntax coloring, automatic indentation and other features

The command mode of the script: you can use the .sh file name as the suffix; in the lower version of the Vim editor, you can only identify whether it is a shell script according to the .sh suffix command, and the higher version of the Vim editor does not need too many file suffixes.

2) shebang: the first line must be shebang, and the interpreter path must occupy the beginning of the opposite line: during execution, start the corresponding interpreter to interpret many commands in the script

Because the shell we use is bash

#! / bin/bash

3) in the shell script, except for shebang, all lines starting with # are comment lines, and the interpreter ignores the contents of such lines

For example:

# author: Qin Yaodong

# type: basic exercise

...

4) blank lines: the interpreter ignores all blank lines in the script

5) A large number of keywords and commands: if, else, then, do, while, for,...

6) all the special characters in bash

/ / Note: once the shell script is run, it opens an interpreter (sub-shell) to interpret and execute the code content in the current shell according to the instructions of shebang. The content of the shell script is implemented in a child shell process.

3. How to run the script:

1) give execution permission to the script file, and run the file directly:

~] # chmod + x / PATH/TO/SCRIPT_FILE

Note: if only the name of the script is written and the path is not written when executing the script, you must make sure that the file can be found under the path saved in the PATH variable

2) run the script directly using the interpreter, saying that the script is used as the parameter of the interpreter command

Bash / PATH/TO/SCRIPT_FILE

Bash-x / PATH/TO/SCRIPT_FILE (showing the running process of a script, usually used for script troubleshooting)

Bash-n / PATH/TO/SCRIPT_FILE (debug the syntax of the script. If there is a syntax error, bash will give you a hint. For this kind of error prompt, we need to judge the location of the error.)

Second, regular expressions:

Before we learn about regular expressions, let's learn about text processing tools:

Text processing three Musketeers:

Grep: grep, egrep, fgrep, text search tools, fuzzy search for given text based on "PATTERN". GREP works in greedy mode by default.

Sed:Stream EDitor, stream editor, line editor, text editing tool

Awk:gawk--GNU awk, text formatting tool, text report generator, programming language for text processing

1.grep system: grep egrep fgrep

1:grep: only basic regular expressions are supported by default

The full name is Global search Regular Expression and Print out the line. Use regular expressions to conduct a global search and display matching lines

1) grep [OPTIONS] PATTERN [FILE...]

/ here we will introduce that PATTERN,PATTERN is a filter condition, which consists of regular expression metacharacters and text characters that have no special meaning.

The metacharacters of regular expressions are interpreted as special meanings by the regular expression engine.

The text character of a regular expression is a character that has only the surface meaning of the character.

Common options are:

-I,-- ignore-case: ignores the case of text characters

-v,-- invert-match: reverse match; the final result is the row that PATTERN cannot match successfully

-c,-- count: count, count all rows that match PATTERN

-o,-- only-matching: turn off greedy mode and show only what PATTERN can match

-Q,-- quiet,-- silent: quiet mode, no matching results are output

-- color [= WHEN],-- colour [= WHEN]: highlight content that matches PATTERN in a special color

-- color=auto

-E,-- extended-regexp: extended regular expression, grep-E is equivalent to egrep

-F,-- fixed-strings,-- fixed-regexp:grep-F is equivalent to fgrep

-G,-- basic-regexp: basic regular expression, egrep-G is equivalent to grep

-P,-- perl-regexp: use the PCRE (Perl Common Regular Expression) engine

-A NUM,-- after-context=NUM: displays the NUM line following the line that matches the PATTERN

-B NUM,-- before-context=NUM: displays the NUM line in front of it while displaying rows that match PATTERN

-C NUM,-NUM,-- context=NUM: displays the NUM lines before and after the lines that match the PATTERN

Metacharacters of basic regular expressions:

1) character matching

. Match any single character

[]: matches any single character in the specified range

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

All of the following character sets can be placed in [] to match a single character

[: lower:]: all lowercase single characters

[: upper:]: all capital letters

[: alpha:]: all alphabetic characters

[: digit:]: all decimal numbers

[: space:]: all white space characters

[: alnum:]: indicates all uppercase and lowercase letters and decimal numbers

[: punct:]: all punctuation marks

[: xdigit:]: all hexadecimal numbers

Amurz: all lowercase letters

Amurz: all capital letters

0-9: identifies all decimal digits

2) matching of times

*: the character in front of it can appear any number of times (0, 1 or more times)

\?: the character in front of it is optional (0 or 1)

\ +: the character preceding it appears at least once (1 or more times)

\ {m\}: the character before it must appear m times

\ {mdirection n\}: the character preceding it appears at least m times and at most n times; (m

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report