shell中常用的基础命令
一、diff
用法:
diff [options] files|directorys #文件比较
输出信息:
[num1,num2][a|c|d][num3,num4]
num1,num2 ##第一个文件中的行
参数 | 说明 |
---|---|
a | 添加 |
c | 更改 |
d | 删除 |
< | 第一个文件中的内容 |
> | 第二个文件中的内容 |
num3,num4 ##第二个文件中的行
常用参数:
参数 | 说明 |
---|---|
-b | 忽略空格 |
-B | 忽略空行 |
-i | 忽略大小写 |
-c | 显示文件所有内容并标示不同 |
-r | 对比目录 |
-u | 合并输出 |
cd /mnt
vim westos #编写内容
cat westos #查看内容
vim westos.new
cat westos.new
diff westos westos.new #比较文件
diff -b westos westos.new #忽略空格
vim westos
cat westos
cat westos.new
diff westos westos.new #比较
vim westos
vim westos.new
cat westos
cat westos.new
diff westos westos.new
diff -b westos westos.new
vim westos
vim westos.new
cat westos
cat westos.new
diff -i westos westos.new #忽略大小写
diff westos westos.new
diff -c westos westos.new #显示文件所有内容并标示不同
mkdir westosdir westosdir1 #创建目录
touch westosdir/jwl #创建文件
diff -r westosdir westosdir1 #对比目录
二、patch
patch 原文件 布丁文件
-b ##备份原文件
dnf install patch -y #安装patch
cat westos
cat westos.new
diff -u westos westos.new > westos.path #合并输出
patch -b westos westos.path #打补丁,加 -b参数备份原文件
ls
cat westos
cat westos.orig
三、cut
cut
-d : ##指定:为分隔符
-f ##指定显示的列 5第五列| 3,5 3和5列|3-5 3到5列|5- 第五列以后|-5 到第五列
-c ##指定截取的字符(数字用法同-f)
ifconfig #查看ip
ifconfig ens3 | cut -d " " -f 10 | head -n 2 | tail -n 1 #列出本机ip
四、sort
参数 | 说明 |
---|---|
-n | 纯数字排序 |
-r | 倒叙 |
-u | 去掉重复 |
-o | 输出到指定文件 |
-t | 指定分割符 |
-k | 指定排序的列 |
五、uniq
参数 | 说明 |
---|---|
-c | 合并重复并统计重复个数 |
-d | 显示重复的行 |
-u | 显示唯一的行 |
实例题
1.ifconfig 网卡 可以显示此网卡的信息
显示信息中包含此网卡使用的ip地址
请用命令过滤此ip并在输出时只显示ip其他信息不显示
两种皆可
ifconfig ens3 | awk '/inet\>/{print $2}'
ifconfig ens3 | cut -d " " -f 10 | head -n 2 | tail -n 1
2.找出能登陆系统用户中UID最大的用户,并显示其名称
两条都可以
grep bash$ /etc/passwd | sort -t : -k 3 -nr | cut -d : -f 1,3| head -n 1
grep bash$ /etc/passwd | sort -t : -k 3 | tail -n 1 | cut -d : -f 1,3
jwl:2008
3.当前主机为web服务器,请抓取访问web服务器次数排在前5的ip地址
cut -d - -f 1 /etc/httpd/logs/access_log | sort -n | uniq -c | sort -nr | head -n 5
六、tr
tr 'a-z' 'A-Z' ##小写转大写
tr 'A-Z' 'a-z' ##大写转小写
七、test
test = [] ##[] 就相当于test命令
"test $a = $b" = [ "$a" = "$b" ]
1.test数字对比
参数 | 说明 |
---|---|
= | |
!= | |
-eq | 等于 |
-ne | 不等于 |
-le | 小于等于 |
-lt | 小于 |
-ge | 大于等于 |
-gt | 大于 |
a=1
b=1
test "$a" = "$b"
test "$a" = "$b" && echo yes || echo no
[ "$a" = "$b" ] && echo yes || echo no
[ "$a" != "$b" ] && echo yes || echo no
[ "$a" -eq "$b" ] && echo yes || echo no
[ "$a" -ne "$b" ] && echo yes || echo no
[ "$a" -le "$b" ] && echo yes || echo no
2.test的条件关系
-a ##并且
-o ##或者
test对空的判定
-n ##nozero 判定内容不为空
-z ##zero 判定内容为空
c=5
[ "$c" -gt "0" -a "$c" -lt "10" ] && echo yes || echo no
c=-2
[ "$c" -gt "0" -a "$c" -lt "10" ] && echo yes || echo no
[ "$c" -lt "0" -a "$c" -gt "10" ] && echo yes || echo no
[ "$c" -lt "0" -o "$c" -gt "10" ] && echo yes || echo no
[ ! "$c" -lt "0" -o "$c" -gt "10" ] && echo yes || echo no
[ -z "$a" ] && echo yes || echo no
echo $a
unset a #删除
echo $a
[ -z "$a" ] && echo yes || echo no
[ -n "$a" ] && echo yes || echo no
3.test对于文件的判定
参数 | 说明 |
---|---|
-ef | 文件节点号是否一致(硬链) |
-nt | 文件1是不是比文件2新 |
-ot | 文件1是不是比文件2老 |
-d | 目录 |
-S | 套结字 |
-L | 软连接 |
-e | 存在 |
-f | 普通文件 |
-b | 快设备 |
-c | 字符设备 |
ls #查看目录文件
ls -i #查看文件节点号
ls -l #列出详细文件
ln /mnt/westos /mnt/westosfile #
ls
ls -i
[ "/mnt/westos" -ef "/mnt/westosfile" ] && echo yes || echo no #比较文件节点号是否一致 ,一致输出yes,反之输出no
[ "/mnt/westos" -nt "/mnt/westosfile" ] && echo yes || echo no #比较文件新旧,文件1是不是比文件2新
[ "/mnt/westos" -ot "/mnt/westosfile" ] && echo yes || echo no #文件1是不是比文件2老
touch westosfile1
[ "/mnt/westos" -ot "/mnt/westosfile1" ] && echo yes || echo no
[ "/mnt/westos" -nt "/mnt/westosfile1" ] && echo yes || echo no
[ -d "/mnt" ] && echo yes || echo no
[ -S "/mnt" ] && echo yes || echo no
ln -s /mnt/westos /mnt/westoslink
ls -l
[ -L "/mnt/westoslink" ] && echo yes || echo no
[ -e "/mnt/westoslink" ] && echo yes || echo no
[ -e "/mnt/westoslin" ] && echo yes || echo no
[ -f "/mnt/westoslink" ] && echo yes || echo no
cat /mnt/westoslink
[ -b "/mnt/westoslink" ] && echo yes || echo no
[ -b "/dev/vdb" ] && echo yes || echo no
[ -c "/dev/vdb" ] && echo yes || echo no
[ -c "/dev/pts/0" ] && echo yes || echo no
4.实例题
文件检测
编写脚本完成以下条件
file_check.sh 在执行时
如果脚本后未指定检测文件报错“未指定检测文件,请指定”
如果脚本后指定文件不存在报错“此文件不存在”
当文件存在时请检测文件类型并显示到输出中
vim file_check.sh
[ -z "$1" ] && {
echo "未检测到文件,请指定"
exit
}
[ -e "$1" ] || {
echo "此文件不存在 "
exit
}
[ -d "$1" ] && {
echo "目录"
exit
}
[ -S "$1" ] && {
echo "套结字"
exit
}
[ -L "$1" ] && {
echo "软连接"
exit
}
[ -f "$1" ] && {
echo "普通文件"
exit
}
[ -b "$1" ] && {
echo "快设设备"
exit
}
[ -c "$1" ] && {
echo "字符设备"
exit
}
sh file_check.sh #未指定文件
sh file_check.sh 12345 #指定不存在的文件
sh file_check.sh westos
sh file_check.sh westoslink
sh file_check.sh lee
八、&& ||
&& 符合条件作动作
|| 不符合条件作动作
脚本访问ip
vim test.sh
ping $1 -c 1 -w 1 &> /dev/null && {
echo -e "\033[32mip is exit\033[0m"
}||{
echo -e "\033[32mip is not exist\033[0m"
}
sh test.sh 172.25.19.254
sh test.sh 172.25.254.19