(一)shell中常用的基础命令

前言

我们常常会用到shell脚本,在我们的学习过程中,比如我们常见的视频脚本、搜索脚本等等,所以从今天就开始学习shell脚本,这一节主要来学习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 ##备份原文件

新建westos并且在里边输入内容,输出重定向在新的一个westosnew中,并且修改其中的内容,用diff命令可以比较两个文件的不同,-c可以输出显示两个文件中的内容,-d是忽略空格,但是由于在之前的修改中,我是修改了内容,所以-d何不加-d的效果一样

[root@westos_student50 mnt]# cat westos > westosnew
[root@westos_student50 mnt]# cat westosnew
hello
mou
great
[root@westos_student50 mnt]# vim westosnew
[root@westos_student50 mnt]# cat westosnew
hello
mousy
great
[root@westos_student50 mnt]# diff westos westosnew
2c2
< mou
---
> mousy
[root@westos_student50 mnt]# diff -b westos westosnew
2c2
< mou
---
> mousy
[root@westos_student50 mnt]# diff -c westos westosnew
*** westos	2022-08-25 21:25:41.355092933 +0800
--- westosnew	2022-08-25 21:27:19.369793728 +0800
***************
*** 1,3 ****
  hello
! mou
  great
--- 1,3 ----
  hello
! mousy
  great

diff-u操作可以将两个文件合并输出

[root@westos_student50 mnt]# diff -u westos westosnew > westosha
[root@westos_student50 mnt]# ls
westos  westosha  westosnew
[root@westos_student50 mnt]# cat westosha
--- westos	2022-08-25 21:25:41.355092933 +0800
+++ westosnew	2022-08-25 21:27:19.369793728 +0800
@@ -1,3 +1,3 @@
 hello
-mou
+mousy
 great

在使用文件修补命令之前,首先要安装patch软件

[root@westos_student50 mnt]# dnf install patch -y

patch命令可以将前边文件按照后边文件进行修改,并且覆盖原文件,但是有的时候我们还是需要保留原文件,所以这个时候我们可以使用patch -b命令,它可以将原文件备份并保存,保存的文件后缀是orig

[root@westos_student50 mnt]# patch westos westosnew 
patch: **** Only garbage was found in the patch input.
[root@westos_student50 mnt]# diff -u westos westosnew > westos.path
[root@westos_student50 mnt]# patch westos westos.path
patching file westos
[root@westos_student50 mnt]# rm -fr westosha
[root@westos_student50 mnt]# ls
westos  westosnew  westos.path
[root@westos_student50 mnt]# cat westos
hello
mousy
great
[root@westos_student50 mnt]# patch -b westosnew westos.path
patching file westosnew
Reversed (or previously applied) patch detected!  Assume -R? [n] y
[root@westos_student50 mnt]# ls
westos  westosnew  westosnew.orig  westos.path

3.cut(数据截取命令)

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

cut命令是数据截取命令,由于在这个文件中:是分隔符,所以-d:指定分隔符是:-f指定要截取的列是哪几列,具体如下:

[root@westos_student50 mnt]# vim passwd
[root@westos_student50 mnt]# cut -d : -f 1 passwd
westos
mysql
apache
nginx
[root@westos_student50 mnt]# cut -d : -f 1-3 passwd
westos:x:1000
mysql:x:27
apache:x:48
nginx:x:975
[root@westos_student50 mnt]# cut -d : -f 1,3 passwd
westos:1000
mysql:27
apache:48
nginx:975

-c指定字符的方法与-f指定列的方法一样
(练习):过滤本机网卡上的ip并且在输出时只显示ip

[root@westos_student50 mnt]# ifconfig ens160 | head -n 2 | tail -n 1 | cut -d ' ' -f 10
192.168.110.30

这个就是先显示前两行,再显示前两行中的最后一行,在这一行里,以空格为分割,第10列就是我们要的本机ens160网卡的ip

4.sort(排序命令)

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

首先编辑文件,并且在文件中写入多行数字,使用sort -n命令就可以对其进行排序

[root@westos_student50 mnt]# sort -n mou

0
0
2
2
3
4
4
5
6
7
7
8
8
12
17

但是我们会发现,在这个多行数字中,有许多时重复的,所以现在我们可以使用-nu来去掉重复的数字

[root@westos_student50 mnt]# sort -nu mou
0
2
3
4
5
6
7
8
12
17

可以使用-nr命令来对文件中的数字进行倒叙排序

[root@westos_student50 mnt]# sort -nr mou
17
12
8
8
7
7
6
5
4
4
3
2
2
0
0

像上边一样,如果不想出现重复的数字,就可以使用-u来进行去重

[root@westos_student50 mnt]# sort -nru mou
17
12
8
7
6
5
4
3
2
0

如果这些数字前边或者后边有相同的部分,那我们就可以使用-t 加分隔符号,-k加我们更容易排序的那一列来进行排序,同样可以使用-u来进行去重

[root@westos_student50 mnt]# vim mou
[root@westos_student50 mnt]# sort -t : -k 2 -n mou

mou:0
mou:0
mou:2
mou:2
mou:3
mou:4
mou:4
mou:5
mou:6
mou:7
mou:7
mou:8
mou:8
mou:12
mou:17
[root@westos_student50 mnt]# sort -t : -k 2 -nu mou
mou:0
mou:2
mou:3
mou:4
mou:5
mou:6
mou:7
mou:8
mou:12
mou:17

5.uniq(重复数据处理)

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

我们可以先将之前文件中的数字进行排序,然后再使用管道符加上重复数据处理命令来对文件中数据进行操作,比如可以筛选出只出现过一次的数字,重复出现过的数字,以及合并统计所有数字以及出现过的次数

[root@westos_student50 mnt]# sort -n mou | uniq -d
0
2
4
7
8
[root@westos_student50 mnt]# sort -n mou | uniq -u

3
5
6
12
17
[root@westos_student50 mnt]# sort -n mou | uniq -c
      1 
      2 0
      2 2
      1 3
      2 4
      1 5
      1 6
      2 7
      2 8
      1 12
      1 17

首先对文件中的数字进行排序,然后将其重复的数字进行合并并且统计次数,再将这个结果以空格为分割选择列进行排序


[root@westos_student50 mnt]# sort -n mou | uniq -c | sort -t  ' ' -k 1 
      1 
      1 12
      1 17
      1 3
      1 5
      1 6
      2 0
      2 2
      2 4
      2 7
      2 8

这个接着上边的结果选择其中最后一列进行输出

[root@westos_student50 mnt]# sort -n mou | uniq -c | sort -t  ' ' -k 1 | tail -n 1
      2 8

命令练习:直接能登录系统的用户中UID最大的用户,并且显示其名称

[root@westos_student50 mnt]# grep bash$ /etc/passwd
root:x:0:0:root:/root:/bin/bash
westos:x:1000:1000::/home/westos:/bin/bash
[root@westos_student50 mnt]# grep bash$ /etc/passwd | sort -t : -k 3 -nr | head -n 1 | cut -d : -f 1 
westos

我们可以看到在etc下的passwd中有一些以bash结尾的命令,这些都是显示能登录我们主机的用户,所以我们就可以对这两行进行处理来获得我们想要的内容:由于我们要获得UID最大的主机名称,所以我们首先可以对UID进行排序,并且选择最大UID的那一行,我们需要的是他的名称,所以我们可以使用cut命令来接去我们想要的用户名称,具体命令如上

6.tr(字符大小写转换)

tr ‘a-z’ ‘A-Z’ ##小写转大写
tr ‘A-Z’ ‘a-z’ ##大写转小写

例如我们在shu文件中写入以下内容

[root@westos_student50 mnt]# cat shu
msy
you need work hard 
and you will find a really good jod next year

如果我们想要将文件中的字母换成大写

[root@westos_student50 mnt]# cat shu | tr 'a-z' 'A-Z'
MSY
YOU NEED WORK HARD 
AND YOU WILL FIND A REALLY GOOD JOD NEXT YEAR

只能转换单个字符,不能转换整个单词或者语句,因为系统会理解为将对应的单个字符全部转换为后边对应的新字符,所以除了本来想要转换的那个单词之外,其他包含这些字符的语句都会将这些字符进行转换,因此,我们只能对单个字符进行转换

[root@westos_student50 mnt]# cat shu | tr 'msy' 'ryj'
ryj
jou need work hard 
and jou will find a reallj good jod next jear

7.test(判定比较命令)

test = [] ##[] 就相当于test命令
“test $a = b " = [ " b" = [ " b"=["a” = “$b” ]
test数字对比
=
!=
-eq ##等于
-ne ##不等于
-le ##小于等于
-lt ##小于
-ge ##大于等于
-gt ##大于
test的条件关系
-a ##并且
-o ##或者
test对空的判定
-n ##nozero 判定内容不为空
-z ##zero 判定内容为空
执行下列脚本来判断用户类型
user_check.sh 用户
用户类型为
super user
system user
common user
test对于文件的判定
-ef ##文件节点号是否一致(硬链)
-nt ##文件1是不是比文件2新
-ot ##文件1是不是比文件2老
-d ##目录
-S ##套结字
-L ##软连接
-e ##存在
-f ##普通文件
-b ##快设备
-c ##字符设备

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

其他详细的比较内容就不一一列举,大多都比较好理解

8.&& ||(条件判断符号)

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

我们可以使用条件判断符号对我们需要判断的条件进行判断并且输出

[root@westos_student50 mnt]# id westos &> /dev/null && echo yes || echo no
yes
[root@westos_student50 mnt]# id user &> /dev/null && echo yes || echo no
no

编写脚本,判断用户是否存在并且输出

#!/bin/bash
id $1 &> /dev/null && {
        echo " $1 is exit "
} || {
        echo " $1 is not exit "
}
~                                                                                                                                                        
~                                   

测试脚本

[root@westos_student50 mnt]# sh test.sh westos
 westos is exit 
[root@westos_student50 mnt]# sh test.sh jkj
 jkj is not exit 

总结

这一节学的基础命令都是之后我们编写脚本用到的基础,比较繁琐,但是都很重要,所以要多多练习,掌握好

  • 1
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值