1,shell中常用的基础命令

## 1.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                         ##合并输出

2.patch

patch 原文件 布丁文件   
-b                ##备份原文件

[root@westoslinux mnt]# dnf install patch

[root@westoslinux mnt]# diff -u westos westos1 > westos.path   ##合并输出
[root@westoslinux mnt]# ls
Apache_port.sh  passwd  westos  westos1  westos.path
[root@westoslinux mnt]# cat westos.path 
--- westos	2021-08-16 09:56:01.282536704 +0800
+++ westos1	2021-08-16 09:56:31.594536704 +0800
@@ -4,3 +4,6 @@
 23
 123
 12
+q
+q
+qqqq
[root@westoslinux mnt]# cat westos
321
12
31
23
123
12
[root@westoslinux mnt]# patch -b westos westos.path
patching file westos

[root@westoslinux mnt]# ls
Apache_port.sh  passwd  westos  westos1  westos.orig  westos.path
[root@westoslinux mnt]# cat westos
321
12
31
23
123
12
q
q
qqqq
[root@westoslinux mnt]# cat westos.orig
321
12
31
23
123
12

3,cut
-d :         ##指定:为分隔符
-f                ##指定显示的列 5第五列| 3,5 3和5列|3-5 3到5列|5- 第五列以后|-5 到第五列

-c         ##指定截取的字符(数字用法同-f)

[root@westoslinux mnt]# cut -d : -f 2 passwd 
x
x
x
x
x
x
x
x
x
x
x
x
x
x
x
[root@westoslinux mnt]# cut -d : -f 2,3 passwd 
x:0
x:1
x:2
x:3
x:4
x:5
x:6
x:7
x:8
x:11
x:12
x:14
x:65534
x:81
x:999
[root@westoslinux mnt]# cut -d : -f 3-5 passwd 
0:0:root
1:1:bin
2:2:daemon
3:4:adm
4:7:lp
5:0:sync
6:0:shutdown
7:0:halt
8:12:mail
11:0:operator
12:100:games
14:50:FTP User
65534:65534:Kernel Overflow User
81:81:System message bus
999:997:systemd Core Dumper

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

[root@westoslinux mnt]# sort -n westos 

1
2
3
4
5
6
7
8
9
[root@westoslinux mnt]# sort -rn westos 
9
8
7
6
5
4
3
2
1

[root@westoslinux mnt]# sort -n westos 
1
1
2
2
3
3
4
4
5
5
6
6
7
8
9
[root@westoslinux mnt]# sort -n westos  -o westos1
[root@westoslinux mnt]# ls
passwd  westos  westos1
[root@westoslinux mnt]# cat westos1
1
1
2
2
3
3
4
4
5
5
6
6
7
8
9
[root@westoslinux mnt]# vim westos
[root@westoslinux mnt]# sort -t : -k 1 westos
1
1:1
2
2:2
3
3:3
4
4:4
5
5:5
6
6:6
7:7
8:8
9
[root@westoslinux mnt]# sort -t : -k 2 westos
1
2
3
4
5
6
9
1:1
2:2
3:3
4:4
5:5
6:6
7:7
8:8

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

[root@westoslinux mnt]# uniq -c westos
      1 1:1
      1 2:2
      1 3:3
      1 4:4
      1 5:5
      1 6:6
      1 7:7
      1 8:8
      1 9
      1 1
      1 2
      1 3
      1 4
      1 5
      1 6
[root@westoslinux mnt]# uniq -c westos1
      2 1
      2 2
      2 3
      2 4
      2 5
      2 6
      1 7
      1 8
      1 9
[root@westoslinux mnt]# uniq -cd westos1
      2 1
      2 2
      2 3
      2 4
      2 5
      2 6
[root@westoslinux mnt]# uniq -d westos1
1
2
3
4
5
6
[root@westoslinux mnt]# uniq -u westos1
7
8
9

测试:

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

ifconfig ens3 | head -n 2 | tail -n 1 | cut -d " " -f 10

172.25.254.171

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

[root@westoslinux mnt]# cat /etc/passwd | grep bash
root:x:0:0:root:/root:/bin/bash
westos:x:1000:1000:westos:/home/westos:/bin/bash
haha:x:7777:7777::/home/haha:/bin/bash
[root@westoslinux mnt]# cat /etc/passwd | grep bash | tail -n 1 | cut -d : -f 1
haha

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

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

7.test
test = [ ]        ##[ ] 就相当于test命令

"test $a = $b" = [ "$a" = "$b" ]

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

[root@westoslinux mnt]# a=1
[root@westoslinux mnt]# b=1
[root@westoslinux mnt]# echo $a
1
[root@westoslinux mnt]# echo $b
1
[root@westoslinux mnt]# [ "$a" = "$b" ] && echo yes || echo no
yes

test的条件关系
-a                ##并且

-o                ##或者

[root@westoslinux mnt]# [ "$a" = "$b" -a "$a" -le "$b" ] && echo yes || echo no
yes
[root@westoslinux mnt]# [ "$a" = "$b" -o "$a" -le "$b" ] && echo yes || echo no
yes

test对空的判定
-n                ##nozero 判定内容不为空

-z                ##zero 判定内容为空


[root@westoslinux mnt]# vim zz.sh 
#1/bin/bash
[ -n "$1" ] && {
  echo 有内容
}||{
  echo 没有内容
}
[root@westoslinux mnt]# echo $c

[root@westoslinux mnt]# echo $a
1
[root@westoslinux mnt]# vim zz.sh 
[root@westoslinux mnt]# sh zz.sh $c
没有内容
[root@westoslinux mnt]# sh zz.sh $a
有内容

测试:

执行下列脚本来判断用户类型
user_check.sh 用户

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

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

#1/bin/bash
[ -z "$1" ] && {
  echo "Error: please input filename following script !!"
  exit
}
[ ! -e "$1" ] && {
  echo "Error: $1 is not exist !!"
  exit
}

[ -L "$1" ] && {
  echo "$1 is link file"
  exit
}
[ -f "$1" ] && {
  echo "$1 is common file"
  exit
}
[ -d "$1" ] && {
  echo "$1 is dirtctory"
  exit
}
[ -b "$1" ] && {
  echo "$1 is block device file"
  exit
}
[ -S "$1" ] && {
  echo "$1 is socket"
  exit
}
[root@westoslinux mnt]# sh zz.sh /mnt
/mnt is dirtctory
[root@westoslinux mnt]# sh zz.sh /mnt/zzz
Error: /mnt/zzz is not exist !!
[root@westoslinux mnt]# sh zz.sh /mnt/zz
Error: /mnt/zz is not exist !!
[root@westoslinux mnt]# sh zz.sh /mnt/westos
/mnt/westos is common file
[root@westoslinux mnt]# sh zz.sh 
Error: please input filename following script !!

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

[root@westoslinux mnt]# [ "$a" = "$b" ] && echo yes || echo no
yes


&& 成功
|| 失败

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值