一、基础命令介绍
1、diff
用法: |
diff [参数] files/directorys |
(常用参数)-b |
忽略空格 |
-B |
忽略空行 |
-i |
忽略大小写 |
-c |
显示文件所有内容并标示不同 |
-r |
对比目录 |
-u |
合并输出(是指把不同的位置合并输出,而不是把所有的内容合并输出) |
输出信息 |
[num1,num2][a |
num1,num2 |
第一个文件中的行 |
a |
添加 |
c |
更改 |
d |
删除 |
< |
第一个文件中的内容 |
> |
第二个文件中的内容 |
num3,num4 |
第二个文件中的行 |
2、patch
用法: |
patch 原文件 补丁文件 |
-b(所加参数) |
备份原文件 |
3、cut
-d: |
指定 :为分隔符 |
-f |
指定显示的列 |
-c |
指定截取的字符 |
4、sort
-n |
纯数字排序 |
-r |
倒序 |
-u |
去掉重复 |
-o |
输出到指定文件 |
-t |
指定分割符 |
-k |
指定排序的列 |
5、uniq
-c |
合并重复并统计重复个数 |
-d |
显示重复的行 |
-u |
显示唯一的行 |
6、tr
tr ‘a-z’ ‘A-Z’ |
小写转大写 |
tr ‘A-Z’ ‘a-z’ |
大写转小写 |
7、test
-eq |
等于 |
-ne |
不等于 |
-le |
小于等于 |
-lt |
小于 |
-ge |
大于等于 |
-gt |
大于 |
-a |
并且 |
– |
– |
-o |
或者 |
-n |
nozero判定内容不为空 |
-z |
zero判定内容为空 |
-ef |
文件节点号是否一致(硬链接) |
-nt |
文件1是不是比文件2新 |
– |
– |
-ot |
文件1是不是比文件2老 |
-d |
目录 |
-S |
套接字 |
-L |
软链接 |
-e |
存在 |
-f |
普通文件 |
-b |
块设备 |
-c |
字符设备 |
8、&& ||
&& %符合条件作动作
|| %不符合条件作动作
二、练习题
1、数据抓取
1.ifconfig 网卡 可以显示此网卡的信息
显示信息中包含此网卡使用的ip地址
请用命令过滤此ip并在输出时只显示ip其他信息不显示
[root@test3 mnt]# ifconfig enp1s0 | head -n 2
enp1s0: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500
inet 10.4.17.100 netmask 255.255.255.0 broadcast 10.4.17.255
[root@test3 mnt]# ifconfig enp1s0 | head -n 2 | tail -n 1
inet 10.4.17.100 netmask 255.255.255.0 broadcast 10.4.17.255
[root@test3 mnt]# ifconfig enp1s0 | head -n 2 | tail -n 1 | cut -d " " -f 10
10.4.17.100
2.找出能登陆系统用户中UID最大的用户,并显示其名称
[root@test3 mnt]# grep bash /etc/passwd | sort -t : -k 3 -n | tail -n 1 | cut -d : -f 1
westos
3.当前主机为web服务器,请抓取访问web服务器次数排在前5的ip地址
[root@test3 mnt]# cat /etc/httpd/logs/access_log | cut -d " " -f 1 | sort -n | uniq -c | sort -nr | head -n 5 | sed 's/^ *//g' | cut -d " " -f 2
10.4.17.216
2、用户类型判断
编写脚本user_check.sh,执行该脚本可判断用户类型:
vim use_check.sh
#!/bin/bash
[ -z "$1" ] && {
echo "Error: Please input username following script !!"
exit
}
id $1 &> /dev/null || {
echo "user $1 is not exist !!"
exit
}
USER_UID=$(id -u $1)
USER_SHELL=$( grep $1 /etc/passwd | cut -d : -f 7)
[ "$USER_UID" -eq "0" ] && {
echo $1 is supper user
exit
}
[ "$USER_UID" -lt "1000" ] && [ ! "$USER_SHELL" = "/bin/bash" ] && {
echo $1 is systemuser
exit
}
[ "$USER_UID" -ge "1000" ] && [ "$USER_SHELL" = "/bin/bash" ] && {
echo $1 is common user
exit
}
echo "unknow user type !!"
3、文件检测
编写脚本完成实现以下功能:
file_check.sh 在执行时,如果脚本后未指定检测文件报错“未指定检测文件,请指定”;如果脚本后指定文件不存在报错“此文件不存在”;当文件存在时请检测文件类型并显示到输出中
vim file_check.sh
#!/bin/bash
[ -z "$*" ] && {
echo "weizhidingjiancewenjian,qingzhiding"
exit
}
[ -e "$*" ] || {
echo "file is not exist"
exit
}
[ -L "$*" ] && {
echo "$*is ruanlianjie"
exit
}
[ -f "$*" ] && {
echo "$*is common file"
exit
}
[ -b "$*" ] && {
echo "$*is dev"
exit
}
[ -c "$*" ] && {
echo "$*is code dev"
exit
}
[ -d "$*" ] && {
echo "$*is dir"
exit
}
[ -S "$*" ] && {
echo "$*is socket"
exit
}
三、文本处理工具grep
1、基本使用及相关参数
grep
grep -E = egrep %加-E表示逻辑匹配
grep 用于处理模式空间中的数据,对原文件不产生影响
grep 匹配条件 处理文件 %使用格式
grep root passwd %过滤root关键字
grep ^root passwd %以root开头
grep root$ passwd %以root结尾
grep -i root passwd %忽略大小写
grep -E "\<root" passwd %root字符之前不能有字符
grep -E "root\>" passwd %root字符之后不能有字符
grep -E "\<root\>" passwd %root字符前后都不能有字符
grep -数字 %显示过滤行以及上面几行和下面几行
grep -n %显示匹配的行所在行号
grep -A %显示过滤行以及下面几行
grep -B %显示过滤行以及上面几行
grep -v %反向过滤
2、grep字符数量匹配规则
^hello %以hello开头
hello$ %以hello结尾
h....o %h开头o结尾中间4个任意字符
[root@test3 mnt]# grep root passwd | grep -E "^root|root$" -v %查找passwd文件中root在中间的
.....s %s结尾前面5个任意字符
* %字符出现任意次
? %0到1次
+ %1次到任意次
{
n} %n次
{
m,n} %m到n次
{
0,n} %0-n次
{
,n} %0-n次
{
m,} %最少m次
(lee){
2} %lee字符串出现2次
3、使用实例:
[root@test3 mnt]# vim passwd
[root@test3 mnt]# grep r.t passwd
operator:x:11:0:operator:/root:/sbin/nologin
rot
[root@test3 mnt]# grep r..t passwd
root:x:0:0:root:/root:/bin/bash
operator:x:11:0:operator:/root:/sbin/nologin
ftp:x:14:50:FTP User:/var/ftp:/sbin/nologin
chroot
rootch
root
testrootttest
[root@test3 mnt]# grep r...t passwd
rooot
testrootttest
[root@test3 mnt]# grep r.*t passwd %查找passwd文件中r和t之间有任意个字符的行
root:x:0:0:root:/root:/bin/bash
operator:x:11:0:operator:/root:/sbin/nologin
ftp:x:14:50:FTP User:/var/ftp:/sbin/nologin
systemd-coredump:x:999:997:systemd Core Dumper:/:/sbin/nologin
systemd-resolve:x:193:193:systemd Resolver:/:/sbin/nologin
chroot
rootch
rot
rooot
roooot
root
testrootttest
4、练习:请显示系统中能被su命令切换的用户名称
grep sh$ /etc/passwd | cut -d : -f 1