I. Review#
sort#
sort
Format: sort options file
-n Sort by numerical value
-r Sort in descending order
-k Specify the column to sort
-t Specify the delimiter
-u Remove duplicates
uniq#
uniq
Format: uniq options file
-c Count occurrences of each line
-u Show only lines that appear once
-d Show only lines that are repeated
cut#
cut
Format: cut options extract range file
-d Specify the delimiter
-f Specify the specific fields to display
-c Specify specific characters
The Three Musketeers of Text#
grep Filter General regular expression analysis program
grep [options]... pattern target file
-i Case insensitive
-v Reverse search, do not display lines containing the specified character
-o Display matching content and show it on a new line
-n Display the line numbers of the filtered lines
-r Recursively search all files in the specified directory (including its subdirectories)
-E Support more extended regular expressions
Regular Expressions#
^aa Lines starting with aa
Aa$ Lines ending with aa
Wildcards#
* Matches the previous item any number of times
? Matches the previous item 0 or 1 time
+ Matches the previous item 1 to many times
. (Placeholder) Matches any character except a newline
{n,m} Matches n to m times
{,n} Matches 0 to n times
{m,} Matches m times or more
[] Set notation
[a-zA-Z]
[0-9]
[^a] Does not match a
Example
---------------------------------------------------------------------------------------------------------------------------------
# grep -E "a.*c" grep_test.txt # Note: .* matches the previous item . 0 times or any number of times
# grep -E "a*c" abc.txt --color=auto # Note: * matches the previous item a 0 times or any number of times
# grep -E "a+c" abc.txt --color=auto # Note: + matches the previous item a 1 time or more
II. wc#
The wc (word count) command
Format: wc [options]... target file...
- -l: Count lines
- -w: Count words (a group of characters surrounded by whitespace)
- -c: Count characters (both visible and invisible characters)
Note: The wc text operation command can directly accept text without needing to use cat
[root@sanchuang-linux ~]# cat wc_test.txt
a b c
aa bb cc
xyz
1234
aa-bb
Example
--------------------------------------------------------------------------------------------
[root@sanchuang-linux ~]# wc -l wc_test.txt # Note: Count lines
5 wc_test.txt
[root@sanchuang-linux ~]# wc -w wc_test.txt # Note: Count words
9 wc_test.txt
[root@sanchuang-linux ~]# wc -c wc_test.txt # Note: Count characters
30 wc_test.txt
--------------------------------------------------------------------------------------------
[root@sanchuang-linux ~]# cat wc_test.txt|wc -c # Method 2: cat
30
[root@sanchuang-linux ~]# wc -c < wc_test.txt # Method 3: Redirection
30
III. diff#
The diff command
- Compares the differences between two files
- The output shows the differences between the two files
Output format of the diff command
- Standard diff
-u
: Will group the differences together for compact readability- -r: Recursively compare all files in the directory
Generate patches using the diff command
diff -u test1 test2 > test.patch
[root@sanchuang-linux ~]# cat diff_1_test.txt
aa
bb
cc
xx
[root@sanchuang-linux ~]# cat diff_2_test.txt
aa
bb
xx
Example 1
--------------------------------------------------------------------------------------------
[root@sanchuang-linux ~]# diff diff_1_test.txt diff_2_test.txt
3d2 # Note: 3d2 means line 3 of file 1 needs to be deleted to match file 2
< cc # Note: cc in file 1
============================================================================================
[root@sanchuang-linux ~]# cat diff_1_test.txt
aa
bb
cc
xx
gg
[root@sanchuang-linux ~]# cat diff_2_test.txt
aa
bb
dd
xx
ee
Example 2
--------------------------------------------------------------------------------------------
[root@sanchuang-linux ~]# diff diff_1_test.txt diff_2_test.txt
3c3 # Note: Line 3
< cc # Note: cc in file 1
---
> dd # Note: dd in file 2
5c5 # Note: Line 5
< gg # Note: gg in file 1
---
> ee # Note: ee in file 2
--------------------------------------------------------------------------------------------
============================================================================================
Example 3: -u: Will group the differences together for compact readability
[root@sanchuang-linux ~]# diff -u diff_1_test.txt diff_2_test.txt
--- diff_1_test.txt 2020-10-30 11:50:45.784010843 +0800
+++ diff_2_test.txt 2020-10-30 11:51:11.475010836 +0800
@@ -1,5 +1,5 @@
aa
bb
-cc # Note: Understand as left - right +
+dd # Note: Or understand as left -cc +dd to match right
xx
-gg
+ee
IV. patch#
The patch command:
- Purpose: Used to apply patches to files
- Format:
patch [options] original file < patch file
-p
N: N indicates the number of path components to ignore-R
: Restore to the old version
Notes
- If applying multiple patches, pay attention to the order
- Do not modify the source file before applying patches
Example
--------------------------------------------------------------------------------------------
[root@sanchuang-linux ~]# yum install patch
[root@sanchuang-linux ~]# diff diff_1_test.txt diff_2_test.txt
3c3 # Note: Difference content
< cc
---
> dd
5c5
< gg
---
> ee
# Note: The difference file is also called a patch file
# Note: The generated file is a patch file for file 1
[root@sanchuang-linux ~]# diff -u diff_1_test.txt diff_2_test.txt > diff_test.patch # Note: Patch for file 1
[root@sanchuang-linux ~]# cat diff_test.patch # Note: Patch file
--- diff_1_test.txt 2020-10-30 11:50:45.784010843 +0800
+++ diff_2_test.txt 2020-10-30 11:51:11.475010836 +0800
@@ -1,5 +1,5 @@
aa
bb
-cc
+dd
xx
-gg
+ee
[root@sanchuang-linux ~]# patch diff_1_test.txt < diff_test.patch # Note: Apply patch
patching file diff_1_test.txt
[root@sanchuang-linux ~]# cat diff_1_test.txt # Note: After applying patch
aa
bb
dd
xx
ee
[root@sanchuang-linux ~]# cat diff_2_test.txt # Note: File 1 and 2 contents are the same
aa
bb
dd
xx
ee
V. grep -A\-B#
-A: Find matching lines and the following few lines
-B: Output matching lines and the preceding few lines
Example
[root@localhost ~]# grep -A 3 quit /etc/passwd # Note: Find matching lines and the following few lines
[root@localhost ~]# grep -B 3 quit /etc/passwd # Note: Output matching lines and the preceding few lines
VI. free -g#
Check memory usage free -g
[root@sanchuang-linux ~]# free -g # Note: Check memory usage, -g unit G, -m unit M
total used free shared buff/cache available
Mem: 1 0 1 0 0 1
Swap: 1 0 1
[root@sanchuang-linux ~]# free -m
total used free shared buff/cache available
Mem: 1800 272 1101 8 426 1363
VII. Writing Scripts#
Implement the following functions
1. Monitor memory usage, if memory usage exceeds 80%, give a reminder
total free usage rate
2. Scan local area network IPs, check which IP addresses are in use
ping -c 1 # Note: Send 1 packet
3. Monitor if the file /etc/passwd has been modified, check every 5 minutes
diff
md5sum # md5 value, unique identifier of the file
4. Monitor if the nginx process exists, if not, give a corresponding reminder
pidof nginx
--------------------------------------------------------------------------------------------
[root@sanchuang-linux ~]# md5sum abc.txt # Note: md5 value
2416b02c3d9d753f48cf49dbb5f1de94 abc.txt
--------------------------------------------------------------------------------------------
[root@sanchuang-linux ~]# pidof nginx # Note: Display the process ID of the specified program
12767 12766 12765
7.1 Monitor memory usage, if memory usage exceeds 80%, give a reminder#
total free usage rate
Example
--------------------------------------------------------------------------------------------
#!/bin/bash
function mem(){
total=`free -m|grep -i mem|tr -s " "|cut -d " " -f2`
#free=`free -m|grep -i mem|tr -s " "|cut -d " " -f4`
used=`free -m|grep -i mem|tr -s " "|cut -d " " -f3`
used_rate=`echo "scale=4;$used/$total" |bc`
#used_1=`echo "$total*0.8"|bc `
result=` echo "$used_rate>0.8"|bc `
echo $result
if (( $result == 1 ))
then
echo -e "\e[31mUsage rate exceeds 80%, please timely expand memory to avoid unnecessary losses\e[0m"
else
echo " nothing to do"
fi
}
mem
--------------------------------------------------------------------------------------------
[root@sanchuang-linux ~]# bash mem_test.sh
0
nothing to do
============================================================================================
Knowledge Point 7.1.1 bc Command
Beginner's Tutorial: https://www.runoob.com/linux/linux-comm-bc.html
The bc command is an arbitrary precision calculator language, usually used as a calculator in linux
[root@localhost ~]# yum install bc -y
[root@localhost ~]# used=`free -m|grep -i mem|tr -s " "|cut -d " " -f3`
[root@localhost ~]# total=`free -m|grep -i mem|tr -s " "|cut -d " " -f2`
[root@localhost ~]# echo "scale=2;$used/$total" |bc # Note: Keep 2 decimal places
.16
[root@localhost ~]# echo "scale=3;$used/$total" |bc # Note: Keep 3 decimal places
.165
--------------------------------------------------------------------------------------------
[root@sanchuang-linux ~]# free -m|grep -i mem|tr -s " "|cut -d " " -f2 # Note: -i case insensitive
1800
[root@sanchuang-linux ~]# use_rate=`echo "scale=4;$used/$total" |bc`
[root@sanchuang-linux ~]# echo "$use_rate>0.8"|bc # Note: Returns 0 for false
0
[root@sanchuang-linux ~]# echo "0.7>0.8"|bc # Note: Returns 0 for false
0
[root@sanchuang-linux ~]# echo "0.9>0.8"|bc # Note: Returns 1 for true
1 # Note: This return should be a boolean value 0 false 1 true, not the value of command execution failure. $? is all 0, command execution successful
############################################################################################
Knowledge Point 7.1.2 Decimal Operations
Decimal operations:
1. Can use bc
[root@sanchuang-linux ~]# echo "scale=3;1/3"|bc # Note: Keep 3 decimal places
.333
[root@sanchuang-linux ~]# echo "0.7>0.8"|bc # Note: Returns 0 for false
0
[root@sanchuang-linux ~]# echo "0.9>0.8"|bc # Note: Returns 1 for true
1
2. awk Options
Syntax: awk options 'pattern+action' file
Common options:
-F Specify the delimiter
Built-in variables
NR Represents the line number in awk
NF Represents the column number in awk
Pattern
Example
--------------------------------------------------------------------------------------------
[root@localhost ~]# free -m
total used free shared buff/cache available
Mem: 3770 195 3274 11 300 3348
Swap: 2047 0 2047
[root@localhost ~]# free -m|awk 'NR==2{print $2}' # Note: Print the second variable of the second line
3770 # Note: NR line number, $2 second variable
[root@localhost ~]# free -m|awk 'NR==2{print $3}' # Note: Print the third variable of the second line
194
============================================================================================
[root@sanchuang-linux ~]# free -m|awk '/Mem/{print $3/$2}' # Note: Calculate decimal, filter out Mem
0.156111 # Note: Filter out this 1 line of Mem
[root@sanchuang-linux ~]# free -m|awk '/Mem/{printf "%.2f\n", $3/$2}' # Note: Keep 2 decimal places
0.16 # Note: \n newline output
7.2 Scan local area network IPs, check which IP addresses are in use#
ping -c 1 # Note: Send 1 packet
Method 1
--------------------------------------------------------------------------------------------
scan_ip(){
for ip in `seq 255`
do
( ip_full=192.168.0.$ip
ping -c 1 $ip_full &>/dev/null && echo $ip_full >>up.txt || echo $ip_full >>down.txt
) & # Note: Execute in a background subprocess
done
wait # Parent process waits for child processes to complete before exiting
}
scan_ip
Method 2
--------------------------------------------------------------------------------------------
scan_ip(){
for ip in 192.168.0.{1..255} # Note: 1-255 can be written this way
do
(
ping -c 1 $ip &>/dev/null && echo $ip >>up.txt || echo $ip >>down.txt
) &
done
wait # Note: Purpose: Parent process waits for child processes to complete before exiting
}
scan_ip
Note: Background processes
Command & Creates a child bash process to execute the command task
wait Parent process waits for child processes to finish before exiting
============================================================================================
[root@sanchuang-linux ~]# ip=45
[root@sanchuang-linux ~]# ip_full=192.168.0.$ip # Note: Shell string concatenation
[root@sanchuang-linux ~]# echo $ip_full
192.168.0.45
[root@sanchuang-linux ~]# top # Note: View CPU
7.3 Monitor if the nginx process exists, if not, give a corresponding reminder#
pidof nginx
Example
--------------------------------------------------------------------------------------------
check_nginx(){
pidof nginx && echo "nginx is running" || echo "nginx is down"
#if [[ $? -eq 0 ]]
#then
# echo "nginx is running"
#fi
}
check_nginx
--------------------------------------------------------------------------------------------
[root@sanchuang-linux ~]# pidof nginx
12767 12766 12765
[root@sanchuang-linux ~]# echo $? # Note: Return value 0 indicates success
0
7.4 Monitor if the file /etc/passwd has been modified, check every 5 minutes#
diff
md5sum # md5 value, unique identifier of the file
Example
--------------------------------------------------------------------------------------------
check_monitor(){
check_num=`differ /etc/passwd /lianxi/passwd |wc -l`
[[ check_num -eq 0 ]] && echo "File has not been modified" || echo "File has been modified"
}
check_monitor
--------------------------------------------------------------------------------------------
[root@sanchuang-linux ~]# cp /etc/passwd /lianxi/passwd
cp: overwrite '/lianxi/passwd'? y
[root@sanchuang-linux ~]# diff /etc/passwd /lianxi/passwd
[root@sanchuang-linux ~]# echo $? # Note: Even if the file is not modified, the return is also 0 (understand as command execution success)
0 # Note: Therefore, cannot directly use a ternary-like operation to judge
[root@sanchuang-linux ~]# diff /etc/passwd /lianxi/passwd
[root@sanchuang-linux ~]# diff /etc/passwd /lianxi/passwd|wc
0 0 0
[root@sanchuang-linux ~]# diff /etc/passwd /lianxi/passwd|wc -l
0
# Note: The basis for judgment is whether diff outputs content
No output, wc -l line count is 0, indicating the file has not been modified
# Note: To check if the file has been modified, think of the diff command