一. 回顾
sort
sort
1 2 3 4 5 6
   | 格式:sort 选项 文件 -n	按数值进行排序 -r	降序排序 -k	指定排序的列 -t	指定分隔符 -u	去重
   | 
uniq
uniq
1 2 3 4
   | 格式:uniq 选项 文件 -c	统计每列在文本中出现的次数 -u	仅显示出现一次的行 -d	仅显示重复出现的行
   | 
cut
cut
1 2 3 4
   | 格式:cut 选项 提取范围 文件 -d	指定分隔符 -f	指定显示的特定字段 -c	指定特定字符
   | 
文本三剑客
1 2 3 4 5 6 7 8
   | grep 过滤 通用的正则表达式分析程序 grep [选项]... 模式 目标文件 -i	不区分大小写 -v	反转查找,不显示包含指定字符的行 -o	显示匹配的内容,并且换行显示 -n	显示出过滤出来的行的行号 -r	递归查找指定目录下所有的文件(包括其子目录) -E	支持更多的正则扩展表达式
   | 
正则表达式
通配符
1 2 3 4 5 6 7
   | *	表示匹配前一项任意次 ?	表示匹配前一次0次或1次 +	表示匹配前一项一次到多次 .	(占位符)表示除换行符之外的任意字符 {n,m}		匹配n到m次 {,n}		匹配0次到n次 {m,}		匹配m次以上
   | 
1 2 3 4
   | []集合表示 [a-zA-Z] [0-9] [^a]		表示不取a
   | 
1 2 3 4 5
   | 示例 ---------------------------------------------------------------------------------------------------------------------------------
 
 
 
   | 
二. wc
wc(字数统计)命令
格式:wc [选项]... 目标文件...
-l:统计行数
-w:统计字数 (前后都是空白的一组字符)
-c:统计字符数(可见和不可见的字符)
注:wc文本操作命令,可以直接接文本,不需要用cat
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
   | [root@sanchuang-linux ~]# cat wc_test.txt  a b c aa bb cc xyz 1234 aa-bb
  示例 -------------------------------------------------------------------------------------------- [root@sanchuang-linux ~]# wc -l wc_test.txt 	 5 wc_test.txt [root@sanchuang-linux ~]# wc -w wc_test.txt 	 9 wc_test.txt [root@sanchuang-linux ~]# wc -c wc_test.txt 	 30 wc_test.txt -------------------------------------------------------------------------------------------- [root@sanchuang-linux ~]# cat wc_test.txt|wc -c		 30 [root@sanchuang-linux ~]# wc -c < wc_test.txt 		 30
   | 
三. diff
diff命令
比较两个文件之间的差异
输出结果为两个文件的不同之处
diff命令的输出格式
标准diff
-u:会将不同的地方放在一起,紧凑易读
-r: 递归比较目录下的所有文件
利用diff命令生成补丁
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56
   | [root@sanchuang-linux ~]# cat diff_1_test.txt aa bb cc xx [root@sanchuang-linux ~]# cat diff_2_test.txt aa bb xx
  示例1 -------------------------------------------------------------------------------------------- [root@sanchuang-linux ~]# diff diff_1_test.txt diff_2_test.txt  3d2							 < cc						 ============================================================================================
  [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
  示例2 -------------------------------------------------------------------------------------------- [root@sanchuang-linux ~]# diff diff_1_test.txt diff_2_test.txt  3c3											 < cc										 --- > dd										 5c5											 < gg										 --- > ee										 -------------------------------------------------------------------------------------------- ============================================================================================
  示例3:-u:会将不同的地方放在一起,紧凑易读 [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											 +dd											  xx -gg +ee
   | 
四. patch
patch命令:
注意事项
如果打多个补丁,注意先后顺序
打补丁前不要修改源文件
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41
   | 示例 -------------------------------------------------------------------------------------------- [root@sanchuang-linux ~]# yum install patch [root@sanchuang-linux ~]# diff diff_1_test.txt diff_2_test.txt 	 3c3							 < cc --- > dd 5c5 < gg --- > ee
 
 
  [root@sanchuang-linux ~]# diff -u diff_1_test.txt diff_2_test.txt > diff_test.patch  [root@sanchuang-linux ~]# cat diff_test.patch 		 --- 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 	 patching file diff_1_test.txt [root@sanchuang-linux ~]# cat diff_1_test.txt 						 aa bb dd xx ee [root@sanchuang-linux ~]# cat diff_2_test.txt 		 aa bb dd xx ee
   | 
五. grep -A\-B
-A:找到匹配行以及后几行
-B:输出匹配行以及前几行
1 2 3
   | 示例 [root@localhost ~]# grep -A 3 quit /etc/passwd		 [root@localhost ~]# grep -B 3 quit /etc/passwd		
   | 
六. free -g
看内存使用率 free -g
1 2 3 4 5 6 7 8
   | [root@sanchuang-linux ~]# free -g		               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 Swap:          2047           0        2047
   | 
七. 编写脚本
1 2 3 4 5 6 7 8 9 10
   | 实现以下功能 1、监控内存使用情况,如果内存使用率大于百分之80,给予提醒 	total free 使用率 2、扫描局域网ip,检查哪些ip地址正在使用 	ping -c 1		 3、监控文件/etc/passwd是否被修改,每隔5分钟监控一次 	diff 	md5sum	 4、监控nginx进程是否存在,不存在就给予相应提醒 	pidof nginx
   | 
1 2 3 4 5 6
   | -------------------------------------------------------------------------------------------- [root@sanchuang-linux ~]# md5sum abc.txt 			 2416b02c3d9d753f48cf49dbb5f1de94  abc.txt -------------------------------------------------------------------------------------------- [root@sanchuang-linux ~]# pidof nginx				 12767 12766 12765
   | 
7.1 监控内存使用情况,如果内存使用率大于百分之80,给予提醒
 total free 使用率
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81
   | 示例 --------------------------------------------------------------------------------------------
  function mem(){     total=`free -m|grep -i mem|tr -s " "|cut -d " " -f2`          used=`free -m|grep -i mem|tr -s " "|cut -d " " -f3`     used_rate=`echo "scale=4;$used/$total" |bc`          result=` echo "$used_rate>0.8"|bc `     echo $result     if (( $result  == 1 ))     then         echo -e "\e[31m使用率超过80%,请及时对内存扩容,以免不必要的损失\e[0m"     else         echo  " nothing to do"     fi } mem -------------------------------------------------------------------------------------------- [root@sanchuang-linux ~]# bash mem_test.sh 0  nothing to do ============================================================================================ 知识点7.1.1 bc 命令 菜鸟教程:https://www.runoob.com/linux/linux-comm-bc.html bc 命令是任意精度计算器语言,通常在linux下当计算器用 [root@localhost ~]# yum install bc -y [root@sanchuang-linux ~]# used=`free -m|grep -i mem|tr -s " "|cut -d " " -f3` [root@sanchuang-linux ~]# total=`free -m|grep -i mem|tr -s " "|cut -d " " -f2` [root@sanchuang-linux ~]# echo "scale=2;$used/$total" |bc	 .16 [root@sanchuang-linux ~]# echo "scale=3;$used/$total" |bc	 .165 -------------------------------------------------------------------------------------------- [root@sanchuang-linux ~]# free -m|grep -i mem|tr -s " "|cut -d " " -f2  1800 [root@sanchuang-linux ~]# use_rate=`echo "scale=4;$used/$total" |bc` [root@sanchuang-linux ~]# echo "$use_rate>0.8"|bc	 0 [root@sanchuang-linux ~]# echo "0.7>0.8"|bc			 0 [root@sanchuang-linux ~]# echo "0.9>0.8"|bc			 1		
  知识点7.1.2 小数的运算 小数的运算: 1、可以使用bc [root@sanchuang-linux ~]# echo "scale=3;1/3"|bc		 .333 [root@sanchuang-linux ~]# echo "0.7>0.8"|bc			 0 [root@sanchuang-linux ~]# echo "0.9>0.8"|bc			 1
  2、awk 选项 语法:awk 选项 ‘模式+动作’ 文件 常用选项: -F	指定分隔符
  内置变量 NR	awk里表示每一行的行号 NF	awk的列号
  模式
  示例 -------------------------------------------------------------------------------------------- [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}'		 3770													 [root@localhost ~]# free -m|awk 'NR==2{print $3}'		 194 ============================================================================================ [root@sanchuang-linux ~]# free -m|awk '/Mem/{print $3/$2}'		 0.156111														 [root@sanchuang-linux ~]# free -m|awk '/Mem/{printf "%.2f\n", $3/$2}'	 0.16															
   | 
7.2 扫描局域网ip,检查哪些ip地址正在使用
 ping -c 1 # 注:发送1个包
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35
   | 方法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         ) &	     done wait  } scan_ip
  方法2 -------------------------------------------------------------------------------------------- scan_ip(){     for ip in 192.168.0.{1..255}					     do         (          ping -c 1 $ip &>/dev/null && echo $ip >>up.txt || echo $ip >>down.txt         ) &      done wait  } scan_ip 注:后台进程 命令 &	   产生子bash进程去执行命令的任务 wait		父进程等待子进程结束之后再退出 ============================================================================================
  [root@sanchuang-linux ~]# ip=45 [root@sanchuang-linux ~]# ip_full=192.168.0.$ip			 [root@sanchuang-linux ~]# echo $ip_full  192.168.0.45 [root@sanchuang-linux ~]# top							
   | 
7.3 监控nginx进程是否存在,不存在就给予相应提醒
 pidof nginx
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
   | 示例 -------------------------------------------------------------------------------------------- check_nginx(){     pidof nginx && echo "nginx is running" || echo "nginx is down"                     } check_nginx -------------------------------------------------------------------------------------------- [root@sanchuang-linux ~]# pidof nginx 12767 12766 12765 [root@sanchuang-linux ~]# echo $?					 0
   | 
7.4 监控文件/etc/passwd是否被修改,每隔5分钟监控一次
 diff
 md5sum # md5值,文件的唯一标识
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
   | 示例 -------------------------------------------------------------------------------------------- check_monitor(){     check_num=`differ /etc/passwd /lianxi/passwd |wc -l`     [[ check_num -eq 0 ]] && echo "文件未被修改" || echo "文件已被修改" } check_monitor -------------------------------------------------------------------------------------------- [root@sanchuang-linux ~]# cp /etc/passwd /lianxi/passwd cp:是否覆盖'/lianxi/passwd'? y [root@sanchuang-linux ~]# diff /etc/passwd /lianxi/passwd [root@sanchuang-linux ~]# echo $?	 0									 [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
  	没有输出内容,wc -l 行数为0,说明文件未被修改
 
   |