文章目录
1、 tee命令
将内容重定向到文件,并且在屏幕上输出
-a 追加输出内容
[root@192 lianxi]# echo 123456 |tee 123.txt
123456
[root@192 lianxi]# cat 123.txt
123456
[root@localhost lianxi]# echo 999999|tee -a 123.txt
999999
[root@localhost lianxi]# cat 123.txt
123456
999999
2、 echo命令
echo - n 内容 输出内容不换行
echo -e 内容 输出内容显示功能(\n 换行 \t 空格)
[root@localhost lianxi]# echo -n "不换行"
不换行[root@localhost lianxi]#
[root@192 lianxi]# echo -e "hello\nworld"
hello
world
[root@192 lianxi]# echo -e "hello\tworld"
hello world
echo 在shell编程的时候主要的作用
- 输出变量和字符串
- 写菜单
[root@192 lianxi]# sh compute.sh
#####################################
1.add 加法
2.sub 减法
3.mul 乘法
4.exit 除法
#####################################
请输入你的选择:1
请输入第一个数字:45
请输入第二个数字:11
45+11 = 56
3、xargs 命令
xargs :将前面命令的输出作为后面命令的参数使用
管道:将前面命令的输出送给后面的命令使用
xargs 需要依赖管道,更加精准,让一下不支持管道的命令也可以使用管道
[root@localhost ~]# echo feng.txt |ls -l
#ls不知道前面传过来的是参数,默认把当前路径作为参数
总用量 4
-rw-------. 1 root root 1084 10月 17 16:24 anaconda-ks.cfg
[root@localhost lianxi]# echo 123.txt |xargs ls -l
-rw-r--r--. 1 root root 14 11月 9 10:24 123.txt
4、seq命令
产生序列的一个命令,经常用在for循环里面控制循环次数
-w 等宽输出
-s 指定分隔符(默认是换行)
[root@localhost lianxi]# seq 4 #产生1-4
1
2
3
4
[root@localhost lianxi]# seq 3 5 #产生2-5
3
4
5
[root@lamp-test lianxi]# seq -w 5 10 #产生5-10 等宽
05
06
07
08
09
10
[root@localhost lianxi]# seq -w -s " " 9 21 #指定分隔符为空格 等宽
09 10 11 12 13 14 15 16 17 18 19 20 21
[root@localhost lianxi]# seq 1 +2 10 #加步长,不加默认是1
1
3
5
7
9
[root@localhost lianxi]# seq 4 -1 1 #逆序,只需要将步长改为-1
4
3
2
1
[root@localhost ~]# seq -s "+" 10| bc #可以用来进行简单的计算
55
扩展:RANDOM产生指定序列
# 随机产生0-4
[root@192 python-test]# echo $(($RANDOM %5))
4
# 随机产生5-9
[root@192 python-test]# echo $(($RANDOM %5 + 5))
5
# 随机产生150-200
[root@192 python-test]# echo $(($RANDOM %50 + 150))
161
扩展:字符串遍历
5、tr命令
实现字符的转换、压缩和删除的功能
tr set1 set2
用set2中的字符替换掉set1中同一位置的字符,只支持管道符或< 或 echo 命令传来的
-d 删除字符
-s 将连续的相同的字符压缩成一个字符
1.df -Th 命令的输出压缩连续的空格
df -Th|tr -s " "
2.df -Th 命令的输出里删除%符号
df -Th|tr -d "%"|grep "/$"|awk '{print $6}'
3.将/etc/hosts文件里的小写字母和数字删除
cat /etc/hosts|tr -d [a-z][0-9]
4.将/etc/passwd文件里的小写字母替换为大写字母
cat /etc/passwd|tr [a-z] [A-Z]
[root@lamp-test lianxi]# w|tr -s " " #将连续的空格压缩成一个空格
[root@localhost lianxi]# echo 112233445566 |tr 123 abc #转换 将相同位置的字符一一替换
aabbcc445566
[root@localhost lianxi]# cat /etc/passwd|tail -1|tr ":" "/"
rose/x/1012/1012///home/rose//bin/bash
[root@localhost lianxi]# echo 11111122222333344444411112222233333|tr -s 123 #压缩
123444444123
[root@localhost lianxi]# echo 111112222333344441111223|tr -s 312 #压缩
123444444123
[root@localhost lianxi]# echo 1122223333333344444111122333|tr -s 123 abc #先替换再压缩
abc444444abc
6、sort命令
linux sort 命令详解
默认按每行的第一个字符排序(默认按ascii码的大小升序排列)
>>> bin(99) #将十进制转换成二进制
'0b1100011'
>>> ord("c") #将c字母转换成对应的ascii码十进制数字
99
>>> chr(99) #将整数转换成对应的ascii码的字母
'c'
-n 按整数进行排序
-r 递减排序
-k 指定哪一列为排序列
-t 指定分隔符
[root@localhost lianxi]# cat grade.txt |sort -k 4 -nr 按英语成绩排序,也可以写成-k4
5 hj 88 94 99
1 cali 80 80 80
3 jack 66 79 71
2 rose 90 77 64
4 tom 81 69 89
id name chinese english math
[root@localhost lianx的i]# cat grade.txt |sort -k 4 -nr|head -3 #取前3名
5 hj 88 94 99
1 cali 80 80 80
3 jack 66 79 71
#显示出uid最大的前5个用户的信息
[root@localhost lianxi]# cat /etc/passwd|sort -t ":" -k 3 -nr|head -5
nobody:x:65534:65534:Kernel Overflow User:/:/sbin/nologin
rose:x:1012:1012::/home/rose:/bin/bash
Bob:x:1011:1011::/home/Bob:/bin/bash
jack:x:1010:1010::/home/jack:/bin/bash
feng:x:1009:1009::/home/feng:/bin/bash
7、uniq命令
去除重复的内容,去重之前要进行排序
否则只会对连续的去重
-c 统计重复的次数
-d 只显示重复的行 (duplicate 重复的)
-u 只显示不重复的行 (unique唯一的)
[root@lamp-test lianxi]# cat grade.txt |awk '{print $4}'|sort -n|uniq -c
1 english #显示重复的并统计重复次数
1 80
2 88
3 90
2 95
2 99
[root@lamp-test lianxi]# cat grade.txt |awk '{print $4}'|sort -n|uniq -d
88 #只显示有重复的
90
95
99
[root@lamp-test lianxi]# cat grade.txt |awk '{print $4}'|sort -n|uniq -u
english #只显示没有重复的
80
8、cut命令
cut -选项 提取范围 文本文件
常见选项:
-c:从指定提取范围中提取字符 --》切片 character
-f:从指定提取范围中提取字段 列 field
-d 指定分隔符
提取范围:
n 第n项
n- 第n项到行尾
-m 行首到第m项
n,m 第n项和第m项
n-m 第n项到第m项
[root@localhost lianxi]# echo abcd123456 |cut -c 3 #截取第3个字符
c
[root@localhost lianxi]# echo abcd123456 |cut -c 1,4 #截取第1和4个字符
ad
[root@localhost lianxi]# echo abcd123456 |cut -c 1-4 #截取1到4个字符
abcd
[root@localhost lianxi]# echo abcd123456 |cut -c -4 #截取第4个字符之前的
abcd
[root@localhost lianxi]# echo abcd123456 |cut -c 4- #截取第4个字符之后的
d123456
[root@localhost lianxi]# df -Th|grep "/$" |tr -d "%"|tr -s ' '
/dev/mapper/cl-root xfs 17G 4.4G 13G 26 /
[root@localhost lianxi]# df -Th|grep "/$" |tr -d "%"|tr -s ' ' |cut -f 6
/dev/mapper/cl-root xfs 17G 4.4G 13G 26 /
[root@localhost lianxi]# df -Th|grep "/$" |tr -d "%"|tr -s ' ' |cut -d ' ' -f 6 #压缩空格后指定分隔符为空格,截取第6列
26
作业:
1.分析 access_log 日志文件里访问次数最多6个ip地址和次数,按照次数的降序排列
[root@localhost ~]# cat access_log |awk ‘{print $1}’|sort -n|uniq
-c|sort -nr|head -6
1001 192.168.0.53
1000 192.168.0.57
865 192.168.0.251
611 192.168.0.32
318 192.168.0.237
293 192.168.0.47
2.分析access_log 日志文件,找出有哪些手机品牌
cat access_log |egrep -i “xiaomi|huawei|iphone”
9、diff命令
只是对比文本文件之间的差异
·输出结果为两个文件的不同之处
diff命令的输出格式
·标准diff
·-u:会将不同的地方放在一起,紧凑易读
·-r: 递归比较目录下的所有文件
利用diff命令生成补丁
·diff -u test1 test2 > test.patch
[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
[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
[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
编写一个脚本,监控/etc/passwd 文件 ,一旦有变化,就在屏幕上输出
#!/bin/bash
mkdir -p /backup
cp /etc/passwd /backup
while :
do
if diff /etc/passwd /backup/passwd &>/dev/null;then
echo "没有改变"
else
echo "/etc/passwd has change 发生了变化"
fi
sleep 1
done
10、patch命令
patch命令:
·用途:用来打补丁修补文件
·格式:patch [选项] 原始文件 < 补丁文件
·-pN: N表示忽略N层路径
·-R: 还原到老版本
注意事项
·如果打多个补丁,注意先后顺序
·打补丁前不要修改源文件
# 生成补丁(将比较结果保存到file_pat.txt )
diff file1 file2 >file_pat.txt
# 打补丁
patch file1 <file_pat.txt
# 回滚撤销补丁
patch -R file1 < file_pat.txt
如何判断一个文件是否变化?
diff、 md5sum
#!/bin/bash
mkdir -p /backup
cp /etc/passwd /backup
while :
do
if [[ $(md5sum /etc/passwd|awk '{print $1}') == $( md5sum /backup/passwd|awk '{print $1}') ]];then
echo "文件未发生改变"
else
echo "文件发生改变"
fi
sleep 2
done