shell 第一章 shell中常用的基础命令

diff

用法: diff   [ options ]   files| directorys

输出信息:
[num1,num2][a|c|d][num3,num4]
num1,num2      ##第一个文件中的行

diff     

     -a  添加
     -c  更改
     -d  删除
      <  第一个文件中的内容
      >  第二个文件中的内容
diff常用参数
-b    忽略空格
-B    忽略空行
-i    忽略大小写
-c    显示文件的所有内容并标示不同
-r    对比目录
-u    合并输出m3,num4   第二个文件中的行

patch

dnf install patch -y
patch  原文件 布丁文件

-b        ##备份原文件

[root@westoslinux112 mnt]# vim westos1
[root@westoslinux112 mnt]# cat westos1
Hello  westos
[root@westoslinux112 mnt]# diff -u westos westos1
--- westos    2021-11-18 09:25:32.037482851 +0800
+++ westos1    2021-11-18 09:25:45.784890083 +0800
@@ -1,3 +1 @@
-hell westos
-123
-456
+Hello  westos
[root@westoslinux112 mnt]# diff -u westos westos1 > westos.path
[root@westoslinux112 mnt]# cat westos.path
--- westos    2021-11-18 09:25:32.037482851 +0800
+++ westos1    2021-11-18 09:25:45.784890083 +0800
@@ -1,3 +1 @@
-hell westos
-123
-456
+Hello  westos
[root@westoslinux112 mnt]# patch -b westos westos.path
patching file westos
[root@westoslinux112 mnt]# ls
test  test1  westos  westos1  westos.orig  westos.path
[root@westoslinux112 mnt]# cat westos.orig
hell westos
123
456

 3.cut

cut
          -d :             ##指定:为分隔符
          -f                ##指定显示的列 5第五列| 3,5 3和5列|3-5 3到5列|5- 第五列以后|-5 到第五列
          -c               ##指定截取的字符(数字用法同-f)

cut -c 1-4 passwd    截取passwd中的1-4列字符

cut -c 1,4 passwd    截取passwd中第一和第四字符

cut -c  -4 passwd     截取passwd中第一到第四字符

cut -c  4- passwd       截取passwd中第四到最后一字符

cut -d :  -f   1-4 passwd     截取passwd中第一列到第四列

cut -d : -f  1,4 passwd    截取passwd中第一列和第四列

cut -d : -f  -4 passwd      截取passwd中第一到第四列

cut -d  :  -f  4- passwd      截取passwd中第四列到最后一列

sort

-n    纯数字排序 
-r    倒叙   
-u    去掉重复 
-o    输入到指定文件 
-t    指定分隔符
-k     指定排序的列

sort westos    按第一个字符排序

sort -n westos   正序

sort -rn westos  倒叙

sort -run westos   去掉重复

sort -run westos -o westos 1    将去掉重复后的导入到westos1中

sort -t : -k 2 -n westos       将第一列排序分割符为:

 uniq

uniq
-c     #合并重复并统计重复个数
-d     #显示重复的行
-u     #显示唯一的行

 sort -n westos | uniq -d 显示重复的行

sort -n westos | uniq -u  显示唯一的行

sort -n westos | uniq -c   合并重复并统计重复个数

 

 测试:

1.ifconfig 网卡 可以显示此网卡的信息
显示信息中包含此网卡使用的ip地址
请用命令过滤此ip并在输出时只显示ip其他信息不显示

[root@westoslinux112 mnt]# sh ifconfig_ens3.sh
172.25.254.112
[root@westoslinux112 mnt]# cat ifconfig_ens3.sh
ifconfig ens3 | head -n2 | tail -n1 | cut -d " " -f 10

2.找出能登陆系统用户中UID最大的用户,并显示其名称

[root@westoslinux112 mnt]# vim UUID.sh
[root@westoslinux112 mnt]# sh UUID.sh
westos
[root@westoslinux112 mnt]# cat UUID.sh
grep -E "bs|bash" /etc/passwd | sort -t : -k 2 -rn | cut -d : -f 1 | head -n 1

3.当前主机为web服务器,请抓取访问web服务器次数排在前5的ip地址

tr

tr 'a-z' 'A-Z'  ##小写转大写
tr 'A-Z' 'a-z'  ##大写转小写

[root@westoslinux112 mnt]# sh test.sh
172.25.254.73 is up
[root@westoslinux112 mnt]# cat test.sh
ping -w1 -c1 172.25.254.73 &> /dev/null && {
      echo 172.25.254.73 is up
}||{
      echo 172.25.254.73 is down
}

 test

test = []      ##[] 就相当于test命令
"test $a = $b" = [ "$a" = "$b" ]  

test "$a" = "$d" && echo yes || echo no

[ "$a" = "$d" ] && echo yes || echo no

test数字对比
=             ##等于
!=            ##不等于
-eq          ##等于
-ne           ##不等于
-le        ##小于等于
-lt         ##小于
-ge      ##大于等于
-gt        ##大于

 test的条件关系
-a       ##并且
-o       ##或者
test对空的判定
-n            ##nozero 判定内容不为空
-z             ##zero 判定内容为空

 

 test对于文件的判定

-ef               ##文件节点号是否一致(硬链)
-nt               ##文件1是不是比文件2新
-ot               ##文件1是不是比文件2老
-d                ##目录
-S               ##套结字
-L                ##软连接
-e                ##存在
-f                ##普通文件
-b               ##快设备
-c               ##字符设备

 

 && ||

&&       符合条件作动作
||          不符合条件作动作

检测:

 编写脚本完成以下条件
file_check.sh 在执行时
如果脚本后未指定检测文件报错“未指定检测文件,请指定”
如果脚本后指定文件不存在报错“此文件不存在”
当文件存在时请检测文件类型并显示到输出中

[root@westoslinux112 mnt]# vim file_check.sh

[root@westoslinux112 mnt]# cat file_check.sh
[ -z "$*" ] && {
  echo "Please input is check file"
  exit
}
 
[ ! -e "$*" ] && {
  echo "$* is not exist"
  exit
}

[ -d "$*" ] && {
  echo "$* is a directory"
  exit
}

[ -S "$*" ] && {
  echo "$* is  a socket"
  exit
}

[ -L "$*" ] && {
  echo "$* is a symbolic link "
  exit
}

[ -f "$*" ] && {
  echo "$* is a regular file"
  exit
}

[ -b "$*" ] && {
  echo "$* is block special"
  exit
}

[ -c "$*" ] && {
  echo "$* character special"
  exit
}

 

猜你喜欢

转载自blog.csdn.net/gk12050802/article/details/121392789