文章目录

  • 前言
  • 1.diff(文件比较命令)
  • 2.patch(文件修补命令)
  • 3.cut(数据截取命令)
  • 4.sort(排序命令)
  • 5.uniq(重复数据处理)
  • 6.tr(字符大小写转换)
    • 7.test(判定比较命令)
  • 8.&& ||(条件判断符号)
  • 总结

前言

我们常常会用到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
! mougreat
--- 1,3 ----hello
! mousygreat

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
+mousygreat

在使用文件修补命令之前,首先要安装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 mou0
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 moumou: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 -u3
5
6
12
17
[root@westos_student50 mnt]# sort -n mou | uniq -c1 2 02 21 32 41 51 62 72 81 121 17

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


[root@westos_student50 mnt]# sort -n mou | uniq -c | sort -t  ' ' -k 1 1 1 121 171 31 51 62 02 22 42 72 8

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

[root@westos_student50 mnt]# sort -n mou | uniq -c | sort -t  ' ' -k 1 | tail -n 12 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 westoswestos is exit
[root@westos_student50 mnt]# sh test.sh jkjjkj is not exit 

总结

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

(一)shell中常用的基础命令相关推荐

  1. Linux让命令居中,Linux 中常用的基础命令

    Linux 中常用的基础命令 ============================================================================== 概述: == ...

  2. linux中cooy命令_Linux:CentOS 7中常用的基础命令

    对于学习Linux系统来说,命令是必须熟练掌握的第一个部分.Linux系统中的命令有600多个,但常用的基础命令并不多.虽然不同版本的Linux系统的命令稍有不同,但命令的语法与使用方法基本相同,因此 ...

  3. Linux:CentOS 7中常用的基础命令

    对于学习Linux系统来说,命令是必须熟练掌握的第一个部分.Linux系统中的命令有600多个,但常用的基础命令并不多.虽然不同版本的Linux系统的命令稍有不同,但命令的语法与使用方法基本相同,因此 ...

  4. Nginx在开发中常用的基础命令

    场景 Ubuntu Server 16.04 LTS上怎样安装下载安装Nginx并启动: https://blog.csdn.net/BADAO_LIUMANG_QIZHI/article/detai ...

  5. shell常用的基础命令

    shell常用的基础命令 1 diff命令 2 patch命令 3 cut命令 4 sort命令 5 uniq 命令 6 tr命令 7 &&和 || 8 test命令 8.1 test ...

  6. Linux最常用的基础命令 下篇

    Linux最常用的基础命令个人总结 shell脚本 脚本就是:写一堆指令存成一个文本,用于完成一些小任务 a="123" linux中定义一个变量 echo $a echo $b ...

  7. 在shell中常用的特殊符号

    在shell中常用的特殊符号罗列如下: # ;   ;; . , / \\ 'string'| !   $   ${}   $? $$   $* \"string\"* **   ...

  8. Linux最常用的基础命令

    Linux最常用的基础命令个人总结 计算机基础知识: 32bit和64bit系统的区别.系统运行机制 32bit=内存的最大寻址空间是2**32,也就是说最大只能使用4GB的内存 64bit=内存的最 ...

  9. Linux最常用的基础命令 上篇

    Linux最常用的基础命令个人总结 计算机基础知识 32bit和64bit系统的区别.系统运行机制 1989年python 诞生 C语言是编译型的语言,不太支持跨平台 Django 江购 32bit= ...

最新文章

  1. Activiti学习——Activiti与Spring集成
  2. shell变量,管道符,作业控制,shell变量,以及变量配置文件
  3. java jtable 监听事件_【Java】在JTable中设置鼠标监听器,点击操作对应数据
  4. Python:高阶函数
  5. linux设备驱动归纳总结(五):3.操作硬件——IO静态映射【转】
  6. java jpa_Java JPA 语法知识
  7. __call__()和call的区别_python中的__init__ 、__new__、__call__小结
  8. java 日期和时间
  9. Android系统信息获取 之三:IMSI号和IMEI解释
  10. BUUCTF-Crypto-rabbit+篱笆墙上的影子(栅栏密码)+RSA题解
  11. 大数据学习开篇:了解大数据导论、清楚大数据应用领域和前景
  12. 如何刷访问量 的详细介绍
  13. url 转码 java_java中URL转码
  14. 网络投票专家投票计算_安全专家说在线投票是一个坏主意。 这就是为什么。
  15. IDEA 一直Updating indexes问题解决
  16. Python绘制饼状图对商品库存进行分析
  17. elasticsearch最大节点数_记录 Elasticsearch 的 maximum shards open 问题
  18. 程序员的高逼格头像——自制八爪鱼少年
  19. jsp/servlet 实现的图书管理系统
  20. Ubuntu查找软件源

热门文章

  1. 虚拟机ifconfig后显示ip过多,无法查看本机ip或看不到全部ip
  2. 大华网络摄像头,查看视频闪烁严重
  3. 51单片机学习笔记-8 DS1302实时时钟
  4. 计算机导论期末考试知识点,计算机导论期末复习(知识点).doc
  5. 大红鹰电子秤60千克tcs60分度值调5克10克
  6. erp服务器和文件服务器,erp是用本地服务器还是云
  7. java 开源网盘_现在的开源网盘还有哪些推荐?
  8. ICCV 2019无人驾驶研究成果大总结(含大量论文及项目数据)
  9. 《微力无边》经典语录
  10. NO.1—Python安装与环境配置