Basic features of Bash (3)
1, provides the programming environment
program = instruction + data
Program Programming Style
Program: command-centric, data-serving instruction
Object type: Data-centric, instruction serves data
Shell program: Provides programming capabilities to interpret execution
How the program is executed:
Computers: Running binary directives
Programming Languages:
Low Level: Compilation
Senior:
Compiling: High-level language--compiler-to-target code (c, C + +, Java)
Explanation: Advanced language--interpreter--Machine code (shell, Perl, Python)
Program Programming:
Sequential execution, loop execution, select execution
Shell programming: procedural, interpreting execution
The basic structure of a programming language: data storage (variables, arrays), expressions, statements
Shell script: Text file
Shebang:
#!/bin/bash
#!/usr/bin/python
#!/usr/bin/perl
Magic number Magic Numbers
To run the script:
1, give the execution permission, through the specific file path to specify the file execution
2. Run the interpreter directly and run the script as a parameter of the interpreter program
Variable: named memory space
Data storage mode: ASCII
Character: 110 converted to binary 24-bit
Value: 110 converted to binary 8-bit (int, float)
The role of variables:
1. Data storage format, 2, participating operation, 3, data range of representation
Type:
Character
Value: Integer, floating-point
Programming Languages:
Strong type: C
Weak type: Bash treats all data to be stored as characters, supports implicit type conversions, does not support floating-point numbers
Logical operation:
True, False (1, 0)
与:1 && 1=11 && 0=00 && 1=00 && 0=0或:1 || 1=11 || 0=10 || 1=10 || 0=0非:!1=0!0=1
Short-circuit Operation:
And:
The first one is 0, and the result must be 0.
[Email protected] ~]# catt/etc/issue && cat/etc/issue
-bash:catt:command not found
[[email protected] ~]# catt/etc/issue && Cat/etc/issuee-bash:catt:command not found the first one for 1, the second must be involved in the operation [[email protected] ~]# cat/etc/issue && echo "true" CentOS release 6.5 (Final) Kernel \ r on an \m true [[email protected] ~]# cat/etc/issue && catt/etc/issue CentOS release 6.5 (Final) Kernel \ r \m-bash:catt:command not found or: the first one is 1, the result must be 1 [[email protected] ~]# cat/etc/issue | | Cat/etc/issue CentOS Release 6.5 (Final) Kernel \ r on an \m [[email protected] ~]# cat/etc/issue | | Catt/etc/issue CentOS Release 6.5 (Final) Kernel \ R on a \m the first is 0, the second must participate in the operation [[email protected] ~]# Cat T/etc/issue | | Cat/etc/issue-bash:catt:command not found CentOS release 6.5 (Final) Kernel \ r on an \m [[Email prote CTED] ~]# Catt/etc/issue | | Cat/etc/issuee-bash:catt:command not found cat:/etc/issuee:no such file or directory
Mixed applications with, or
[Email protected] ~]# cat/etc/issue &>/dev/null && echo "true" | | echo "false"
True
[Email protected] ~]# catt/etc/issue &>/dev/null && echo "true" | | echo "false"
False
Grep:
The Three musketeers of text processing on Linux
grep Text Filter tool (pattern: pattern)
grep, Egrep, Fgrep
Sed stream editor Text Editing Tool
Implementation of the Gawk Text Report generator on awk Linux
Grep:global search expression and print out of the line.
Function: Text Search tool, according to user-specified "mode" to match the target text line by row to check, print the matching line;
Patterns: Filter conditions written by regular expression characters and text characters
REGEXP:由一类特殊字符及文本字符所编写的模式,其中有些字符不表示字符字面意义,而表示控制或通配的功能;分两类:基本正则表达式:BRE扩展正则表达式:ERE grep -E,egrep
Regular expression engine
grep [options] pattern [file ...]
Options:
--color=auto, coloring the text to match to the display
[[email protected] ~]# grep--color=auto "root"/etc/passwd
Root:x:0:0:root:/root:/bin/bash
Operator:x:11:0:operator:/root:/sbin/nologin
-V: Shows rows that cannot be matched to pattern
[Email protected] ~]# grep-v "abc"/etc/issue
CentOS Release 6.5 (Final)
Kernel \ r on an \m
-I: Ignore character case
[Email protected] ~]# grep-i "CentOS"/etc/issue
CentOS Release 6.5 (Final)
-O: Show only the matching string
[Email protected] ~]# grep-o "release"/etc/issue
Release
-Q: Silent mode, does not output any information
[Email protected] ~]# grep-q "release"/etc/issue
[[email protected] ~]# echo $?
0
-A #: Displays the matching lines, appends the following # lines, and does not display if there is no text content later, indicating after
[[email protected] ~]# grep-a 2 "root"/etc/passwd
Root:x:0:0:root:/root:/bin/bash
Bin:x:1:1:bin:/bin:/sbin/nologin
Daemon:x:2:2:daemon:/sbin:/sbin/nologin
Operator:x:11:0:operator:/root:/sbin/nologin
Games:x:12:100:games:/usr/games:/sbin/nologin
Gopher:x:13:30:gopher:/var/gopher:/sbin/nologin
-B #: Displays the matching lines, appends the previous # lines, and does not appear if there is no text content in front of it, indicating before
[[email protected] ~]# grep-b 2 "root"/etc/passwd
Root:x:0:0:root:/root:/bin/bash
Mail:x:8:12:mail:/var/spool/mail:/sbin/nologin
Uucp:x:10:14:uucp:/var/spool/uucp:/sbin/nologin
Operator:x:11:0:operator:/root:/sbin/nologin
-C #: Displays the rows that match, appends the lines of the rows before and after the display, and does not display if there is no text content, indicating context
[[email protected] ~]# grep-c 2 "root"/etc/passwd
Root:x:0:0:root:/root:/bin/bash
Bin:x:1:1:bin:/bin:/sbin/nologin
Daemon:x:2:2:daemon:/sbin:/sbin/nologin
Mail:x:8:12:mail:/var/spool/mail:/sbin/nologin
Uucp:x:10:14:uucp:/var/spool/uucp:/sbin/nologin
Operator:x:11:0:operator:/root:/sbin/nologin
Games:x:12:100:games:/usr/games:/sbin/nologin
Gopher:x:13:30:gopher:/var/gopher:/sbin/nologin
-e: Using the ere extension regular expression
Basic Regular Expressions
Character Matching:
.: Matches any single character
[[email protected] ~]# grep "."/tmp/abc
A
B
C
[]: matches any single character within the specified range
[Email protected] ~]# grep [ABC]/tmp/abc
A
B
C
[^]: matches any single character outside the specified range
[[email protected] ~]# grep [^ABC]/tmp/abc
D
E
[:d Igit:]: matches any single number
[[email protected] ~]# grep [[:d igit:]]/tmp/abc
1
2
3
[: Lower:]: matches any single lowercase letter
[[email protected] ~]# grep [[: Lower:]]/tmp/abc
A
B
C
[: Upper:]: matches any single uppercase letter
[[email protected] ~]# grep [[: Upper:]]/tmp/abc
A
B
C
[: Alpha:]: Matches any single case letter
[[email protected] ~]# grep [[: Alpha:]]/tmp/abc
A
B
C
A
B
C
[: Alnum:]: Matches any single case letter or number
[[email protected] ~]# grep [[: Alnum:]]/tmp/abc
A
B
C
1
2
3
A
B
C
[:p UNCT:]: matches any single punctuation
[[email protected] ~]# grep [[:p UNCT:]]/tmp/abc
,
.
?
[: Space:]: Match spaces
Number of matches: used after the character to be specified, to specify the number of occurrences of the preceding character
: matches the preceding character any time, matches as much as possible, greedy mode
[[email protected] ~]# grep "aB"/tmp/abc
B
B
Ab
AaB
Aaab
. : Any character of any length
[[email protected] ~]# grep "." /tmp/abc
A
B
C
\?: matches the preceding character 0 or 1 times, that is, the preceding character is optional
[[email protected] ~]# grep--color=auto "A\?b"/tmp/abc
B
B
Ab
AaB
Aaab
Xyab
Xyxyab
Xyxyxyab
+: Match the characters in front of it at least 1 times;
[[email protected] ~]# grep "A+b"/tmp/abc
Ab
AaB
Aaab
{m}: matches the preceding character m times
[[email protected] ~]# grep "A{3}b"/tmp/abc
Aaab
{M,n}: matches the preceding character at least m times, up to N times
[[email protected] ~]# grep "A{1,3}b"/tmp/abc
Ab
AaB
Aaab
{0,n}: matches the preceding character up to n times
[[email protected] ~]# grep "A{0,3}b"/tmp/abc
B
B
Ab
AaB
Aaab
{m,}: matches the preceding character at least m times
[[email protected] ~]# grep "A{2,}b"/tmp/abc
AaB
Aaab
Location anchoring:
^: Anchor at the beginning of the line for the leftmost mode
[[email protected] ~]# grep "^root"/etc/passwd
Root:x:0:0:root:/root:/bin/bash
$: End-of-line anchoring for the right-most mode
[[email protected] ~]# grep "bash$"/etc/passwd
Root:x:0:0:root:/root:/bin/bash
Mary:x:503:503:i am mary.:/ Home/mary:/bin/bash
Centos:x:504:504::/tmp/centos:/bin/bash
Test:x:505:505::/tmp/test:/bin/bash
Rocket:x:507:507::/home/rocket:/bin/bash
^pattern$: User mode matches whole line
[[email protected] ~]# grep "^ab$"/tmp/abc
Ab
^$: Match blank line
\< or \b: The first anchor of the word, used for the left side of the word pattern
[[email protected] ~]# grep "\<root"/etc/passwd
Root:x:0:0:root:/root:/bin/bash
Operator:x:11:0:operator:/root:/sbin/nologin
\> or \b: The ending anchor for the right side of the word pattern
[[email protected] ~]# grep "bash\>"/etc/passwd
Root:x:0:0:root:/root:/bin/bash
Mary:x:503:503:i am mary.:/ Home/mary:/bin/bash
Centos:x:504:504::/tmp/centos:/bin/bash
Test:x:505:505::/tmp/test:/bin/bash
Rocket:x:507:507::/home/rocket:/bin/bash
\<pattern\>: Match Whole word
[[email protected] ~]# grep "\<aaab\>"/tmp/abc
Aaab
Group:
(): Bind one or more characters together and treat them as a whole
[[email protected] ~]# grep--color=auto "(XY) *ab"/tmp/abc
Ab
AaB
Aaab
Xyab
Xyxyab
Xyxyxyab
Note: The patterns in the grouping brackets match to the contents that are recorded by the regular expression engine with internal variables that are named by: \1,\2,\3 ...
\1: The character that matches the pattern between the left side, the first opening parenthesis, and the matching closing parenthesis
(ab+ (XY))
\1:ab+ (XY)
\2:xy
Back reference: Refers to the character that matches the pattern in the preceding grouping brackets (not the pattern itself)
[[email protected] ~]# grep--color=auto "(XY) *ab\1+"/tmp/abc
Xyabxy
Xyxyabxyxy
Xyxyxyabxyxyxy
Practice:
1. Display the line in the/proc/meminfo file that starts with the size s (requires: use two ways)
grep "^[ss]"/proc/meminfo
Grep-i "^s"/proc/meminfo
2. Display the lines in the/etc/passwd file that do not end in/bin/bash;
Grep-v "/bin/bash$"/etc/passwd
3. Display the user name of the user with the largest ID number in the/etc/passwd file;
cat/etc/passwd | SORT-T:-k3-n | Tail-1 | Cut-d:-f1
4, if the user root exists, display its default shell program;
cat/etc/passwd | SORT-T:-k3-n | Tail-1 | Cut-d:-f1
SORT-T:-k3-n/etc/passwd | Tail-1 | Cut-d:-f1
5. Find out the number of two or three digits in/etc/passwd;
ID root &>/dev/null && grep "^root\>"/etc/passwd | Cut-d:-f7
grep "^root\>"/etc/passwd &>/dev/null && grep "^root\>"/etc/passwd | Cut-d:-f7
6. Display the line in the/etc/rc.d/rc.sysinit file with at least one whitespace character and the following non-whitespace characters;
grep "^[[:space:]]+[^[:space:]]"/etc/rc.d/rc.sysinit
7. Find the line ending with "LISTEN" followed by 0, 1 or more whitespace characters in the result of "Netstat-tan" command;
Netstat-tan | grep "LISTEN ([[: Space:]])$"
Netstat-tan | grep "Listen[[:space:]]$"
8, add user bash, Testbash, basher and Nologin (its shell is/sbin/nologin), and then find the/etc/passwd file in the same shell name line;
Useradd Bash
Useradd Testbash
Useradd basher
Useradd-s/sbin/nologin Nologin
[[email protected] ~]# tail -4 /etc/passwdbash:x:601:601::/home/bash:/bin/bashtestbash:x:602:602::/home/testbash:/bin/bashbasher:x:603:603::/home/basher:/bin/bashnologin:x:604:604::/home/nologin:/sbin/nologingrep "\([[:alnum:]]\+\).*\1\?" /etc/passwd 错误做法grep "^\([[:alnum:]]\{1,\}\>\).*\1$" /etc/passwdgrep "^\([[:alnum:]]\{1,\}\)\>.*\1$" /etc/passwdgrep "\(\<[[:alnum:]]\{1,\}\>\).*\1$" /etc/passwdgrep "\(\<[[:alnum:]]\+\>\).*\1$" /etc/passwd
Practice:
1, write a script to achieve the following functions
If the User1 user exists, it is shown to exist, otherwise added;
Displays information such as the ID number of the added user
[Email protected] test]# cat/tmp/test/a.sh
[Email protected] test]# cat/tmp/test/a.sh
#!/bin/bash
id user1 &> /dev/null && echo "user1 exists." || useradd user1grep "\<user1\>" /etc/passwdid user1[[email protected] test]# bash /tmp/test/a.shuser1:x:605:605::/home/user1:/bin/bashuid=605(user1) gid=605(user1) groups=605(user1)[[email protected] test]# bash /tmp/test/a.shuser1 exists.user1:x:605:605::/home/user1:/bin/bashuid=605(user1) gid=605(user1) groups=605(user1)
2, write a script to complete the following functions
If the root user is logged on to the current system, the root user is shown online, otherwise it is not logged in
[Email protected] test]# cat/tmp/test/b.sh
#!/bin/bash
who | grep "^root\>" &> /dev/null && echo "user online." || "user no login."[[email protected] test]# bash /tmp/test/b.shuser online.
Shell programming Preliminary, grep, and regular expressions