mycpen

Mycpen

记录学习历程与受益知识
github
telegram
bilibili

13_Linux基础-SHELL命令-wc-diff-patch-bc-awk

一。回顾#

sort#

sort

格式:sort 选项 文件
-n	按数值进行排序
-r	降序排序
-k	指定排序的列
-t	指定分隔符
-u	去重

uniq#

uniq

格式:uniq 选项 文件
-c	统计每列在文本中出现的次数
-u	仅显示出现一次的行
-d	仅显示重复出现的行

cut#

cut

格式:cut 选项 提取范围 文件
-d	指定分隔符
-f	指定显示的特定字段
-c	指定特定字符

文本三剑客#

grep 过滤 通用的正则表达式分析程序
grep [选项]... 模式 目标文件
-i	不区分大小写
-v	反转查找,不显示包含指定字符的行
-o	显示匹配的内容,并且换行显示
-n	显示出过滤出来的行的行号
-r	递归查找指定目录下所有的文件(包括其子目录)
-E	支持更多的正则扩展表达式

正则表达式#

^aa	以aa开头的行
Aa$	以aa结尾的行

通配符#

*	表示匹配前一项任意次
?	表示匹配前一次0次或1次
+	表示匹配前一项一次到多次
.	(占位符)表示除换行符之外的任意字符
{n,m}		匹配n到m次
{,n}		匹配0次到n次
{m,}		匹配m次以上
[]集合表示
[a-zA-Z]
[0-9]
[^a]		表示不取a
示例
---------------------------------------------------------------------------------------------------------------------------------
# grep -E "a.*c" grep_test.txt 				# 注:.* 匹配前一项 . 0次或任意次
# grep -E "a*c" abc.txt --color=auto		# 注:*  匹配前一项 a 0次或任意次
# grep -E "a+c" abc.txt --color=auto		# 注:+  匹配前一项 a 1次或多次

二. wc#

wc(字数统计)命令

格式:wc [选项]... 目标文件...

  • -l:统计行数
  • -w:统计字数 (前后都是空白的一组字符)
  • -c:统计字符数(可见和不可见的字符)

注:wc 文本操作命令,可以直接接文本,不需要用 cat

[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		# 写法2:cat
30
[root@sanchuang-linux ~]# wc -c < wc_test.txt 		# 写法3:重定向
30

三. diff#

diff 命令

  • 比较两个文件之间的差异
  • 输出结果为两个文件的不同之处

diff 命令的输出格式

  • 标准 diff
  • -u:会将不同的地方放在一起,紧凑易读
  • -r: 递归比较目录下的所有文件

利用 diff 命令生成补丁

  • 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

示例1
--------------------------------------------------------------------------------------------
[root@sanchuang-linux ~]# diff diff_1_test.txt diff_2_test.txt 
3d2							# 注:3d2  文件1个第3行 需要删除 就会和 文件2相同
< cc						# 注:文件1 中的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											# 注:第3行
< cc										# 注:文件1 中的cc
---
> dd										# 注:文件2 中的dd
5c5											# 注:第5行
< gg										# 注:文件1 中的gg
---
> ee										# 注:文件2 中的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											# 注:或者理解为 左边 -cc +dd 就和右边相同
 xx
-gg
+ee

四. patch#

patch 命令:

  • 用途:用来打补丁修补文件
  • 格式:patch [选项] 原始文件 < 补丁文件
  • -pN: N 表示忽略 N 层路径
  • -R: 还原到老版本

注意事项

  • 如果打多个补丁,注意先后顺序
  • 打补丁前不要修改源文件
示例
--------------------------------------------------------------------------------------------
[root@sanchuang-linux ~]# yum install patch
[root@sanchuang-linux ~]# diff diff_1_test.txt diff_2_test.txt 	
3c3							# 注:差异内容
< cc
---
> dd
5c5
< gg
---
> ee

#注:差异文件又叫补丁文件
#注:生成的是文件1的补丁文件
[root@sanchuang-linux ~]# diff -u diff_1_test.txt diff_2_test.txt > diff_test.patch # 注:1的补丁
[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 		# 注:文件1、2内容相同
aa
bb
dd
xx
ee

五. grep -A\-B#

-A:找到匹配行以及后几行

-B:输出匹配行以及前几行

示例
[root@localhost ~]# grep -A 3 quit /etc/passwd		# 注:找到匹配行以及后几行
[root@localhost ~]# grep -B 3 quit /etc/passwd		# 注:输出匹配行以及前几行

六. free -g#

看内存使用率 free -g

[root@sanchuang-linux ~]# free -g		# 注:看内存使用率 ,-g单位G , -m单位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
Swap:          2047           0        2047

七。编写脚本#

实现以下功能
1、监控内存使用情况,如果内存使用率大于百分之80,给予提醒
	total free 使用率
2、扫描局域网ip,检查哪些ip地址正在使用
	ping -c 1		# 注:发送1个包
3、监控文件/etc/passwd是否被修改,每隔5分钟监控一次
	diff
	md5sum	# md5值,文件的唯一标识
4、监控nginx进程是否存在,不存在就给予相应提醒
	pidof nginx
--------------------------------------------------------------------------------------------
[root@sanchuang-linux ~]# md5sum abc.txt 			# 注:md5值
2416b02c3d9d753f48cf49dbb5f1de94  abc.txt
--------------------------------------------------------------------------------------------
[root@sanchuang-linux ~]# pidof nginx				# 注:显示指定程序的进程号
12767 12766 12765

7.1 监控内存使用情况,如果内存使用率大于百分之 80,给予提醒#

​ total free 使用率

示例
--------------------------------------------------------------------------------------------
#!/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[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	# 注:保留2位小数
.16
[root@sanchuang-linux ~]# echo "scale=3;$used/$total" |bc	# 注:保留3位小数
.165
--------------------------------------------------------------------------------------------
[root@sanchuang-linux ~]# free -m|grep -i mem|tr -s " "|cut -d " " -f2 # 注:-i 不区分大小写
1800
[root@sanchuang-linux ~]# use_rate=`echo "scale=4;$used/$total" |bc`
[root@sanchuang-linux ~]# echo "$use_rate>0.8"|bc	# 注:为假返回0
0
[root@sanchuang-linux ~]# echo "0.7>0.8"|bc			# 注:为假返回0
0
[root@sanchuang-linux ~]# echo "0.9>0.8"|bc			# 注:为真返回1
1		# 注:这返回的应该是布尔值0假1真,而不是命令执行失败的值。$?都为0 ,命令执行成功
############################################################################################
知识点7.1.2 小数的运算
小数的运算:
1、可以使用bc
[root@sanchuang-linux ~]# echo "scale=3;1/3"|bc		# 注:保留3位小数
.333
[root@sanchuang-linux ~]# echo "0.7>0.8"|bc			# 注:不成立返回0
0
[root@sanchuang-linux ~]# echo "0.9>0.8"|bc			# 注:成立返回1
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}'		# 注:打印第二行 第2个变量
3770													# 注:NR 行号 , $2 第2个变量
[root@localhost ~]# free -m|awk 'NR==2{print $3}'		# 注:打印第二行 第3个变量
194
============================================================================================
[root@sanchuang-linux ~]# free -m|awk '/Mem/{print $3/$2}'		# 注:计算小数,过滤出Mem
0.156111														# 注:过滤出Mem 这1行
[root@sanchuang-linux ~]# free -m|awk '/Mem/{printf "%.2f\n", $3/$2}'	# 注:保留2位的浮点数
0.16															# 注:\n换行输出

7.2 扫描局域网 ip, 检查哪些 ip 地址正在使用#

​ ping -c 1 # 注:发送 1 个包

方法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}					# 注: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			# 注:shell 字符串的拼接
[root@sanchuang-linux ~]# echo $ip_full 
192.168.0.45
[root@sanchuang-linux ~]# top							# 注:查看cpu

7.3 监控 nginx 进程是否存在,不存在就给予相应提醒#

​ pidof nginx

示例
--------------------------------------------------------------------------------------------
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 $?					# 注:返回值为0表示成功
0

7.4 监控文件 /etc/passwd 是否被修改,每隔 5 分钟监控一次#

​ diff

​ md5sum # md5 值,文件的唯一标识

示例
--------------------------------------------------------------------------------------------
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 (理解为命令执行成功)
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
#注:判定依据 diff 是否输出内容
	没有输出内容,wc -l 行数为0,说明文件未被修改
#注:文件是否被修改,想到diff命令

Loading...
Ownership of this post data is guaranteed by blockchain and smart contracts to the creator alone.