• diff 和 patch
    命令帮助:diff –help | patch –help

    • diff命令在最简单的情况下,比较给定的两个文件的不同。如果使用“-”代替“文件”参数,则要比较的内容将来自标准输入。diff命令是以逐行的方式,比较文本文件的异同处。如果该命令指定进行目录的比较,则将会比较该目录中具有相同文件名的文件,而不会对其子目录文件进行任何比较操作。
    • patch 命令用于打补丁,补丁文件是使用diff产生的,
      patch 命令失败或拒绝接受补丁时,会产生一个和原文件同名,以”.rej”为后缀的差异文件。
         当知道 -b 时,会产生一个和原文件同名,以”.orig”为后缀的备份文件。
[root@localhost mnt]# vim tutu
[root@localhost mnt]# cat tutu
hello 123
[root@localhost mnt]# vim tutu1
[root@localhost mnt]# cat tutu1
hello 123
I like tutu
[root@localhost mnt]# diff tutu tutu1
1a2                  ##表示文件 tutu 与文件 tutu1 相比,少了第2行内容
> I like tutu
[root@localhost mnt]# diff tutu1 tutu
2d1                  ##表示文件 tutu1 与文件t utu 相比,多了第2行内容
< I like tutu
[root@localhost mnt]# vim tutu
[root@localhost mnt]# cat tutu
hello 123
butterbly
[root@localhost mnt]# diff tutu tutu1
2c2                  ##表示文件 tutu 与文件 tutu1 相比,第二行内容有所不同
< butterbly
---
> I like tutu
[root@localhost mnt]# diff -u tutu tutu1
## -u —— 以合并的方式来显示文件内容的不同。
--- tutu    2018-06-12 03:07:02.821689949 -0400
+++ tutu1   2018-06-12 03:05:49.009689949 -0400
@@ -1,2 +1,2 @@hello 123
-butterbly
+I like tutu
[root@localhost mnt]# ls
tutu  tutu1
[root@localhost mnt]# diff -u tutu tutu1 > tutu.path
##把用合并的方式显示的文件内容的不同导出到文件 tutu.path
[root@localhost mnt]# cat tutu.path
--- tutu    2018-06-12 03:07:02.821689949 -0400
+++ tutu1   2018-06-12 03:05:49.009689949 -0400
@@ -1,2 +1,2 @@hello 123
-butterbly
+I like tutu
[root@localhost mnt]# ls
tutu  tutu1  tutu.path

**打补丁**

  • [root@localhost mnt]# yum install patch -y安装补丁工具
[root@localhost mnt]# vim tutu
[root@localhost mnt]# cat tutu
hello 123
butterfly
[root@localhost mnt]# vim tutu1
[root@localhost mnt]# cat tutu1
hello 123
I like tutu
[root@localhost mnt]# diff -u tutu tutu1 > tutu.path
[root@localhost mnt]# ls
tutu  tutu1  tutu.path
[root@localhost mnt]# patch -b tutu tutu.path
##给文件 tutu 打补丁, -b —— 备份源文件
patching file tutu
[root@localhost mnt]# ls
tutu  tutu1  tutu.orig  tutu.path
[root@localhost mnt]# cat tutu.path
--- tutu    2018-06-12 03:39:26.522689949 -0400
+++ tutu1   2018-06-12 03:39:56.100689949 -0400
@@ -1,2 +1,2 @@hello 123
-butterfly
+I like tutu
[root@localhost mnt]# cat tutu
hello 123
I like tutu
[root@localhost mnt]# cat tutu1
hello 123
I like tutu
[root@localhost mnt]# cat tutu.orig
hello 123
butterfly
[root@localhost mnt]
  • cut

cut命令可以从一个文本文件或者文本流中提取文本列。
命令用法:

  • cut -b filename
  • cut -c filename
  • cut -f filename
  • -b —— 字节、-c —— 字符、-f —— 字段

命令和脚本实现查看 IP

[root@localhost mnt]# ifconfig eth0
eth0: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500inet 172.25.254.127  netmask 255.255.255.0  broadcast 172.25.254.255inet6 fe80::5054:ff:fe00:430a  prefixlen 64  scopeid 0x20<link>ether 52:54:00:00:43:0a  txqueuelen 1000  (Ethernet)RX packets 2467  bytes 2944534 (2.8 MiB)RX errors 0  dropped 0  overruns 0  frame 0TX packets 5207  bytes 383810 (374.8 KiB)TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0
[root@localhost mnt]#[root@localhost mnt]# ifconfig eth0 | head -n 2 | tail -n 1 | cut -d " " -f 10
172.25.254.127   ##不建议使用
[root@localhost mnt]# ifconfig eth0 | awk -F " " '/inet\>/{print $2}'
172.25.254.127   ##建议使用
[root@localhost mnt]# vim ip_show.sh   ##脚本实现
[root@localhost mnt]# cat ip_show.sh
##################################
# Author:        tutu            #
# Version:                       #
# Mail:                          #
# Date:          2018-06-12      #
# Description:                   #
#                                #
###################################!/bin/bash
ifconfig eth0 | awk -F " " '/inet\>/{print $2}'
[root@localhost mnt]# sh ip_show.sh
172.25.254.127
  • sort 和 uniq

sort命令是在Linux里非常有用,它将文件进行排序,并将排序结果标准输出。

  • sort
    多用于字符排序
    sort -n ##纯数字排序
    sort -r ##倒序
    sort -u ##去掉重复数字
    sort -o ##输出到指定文件中
    sort -t ##指定分隔符
    sort -k ##指定要排序的列

uniq命令用于报告或忽略文件中的重复行,一般与sort命令结合使用

  • uniq
    对重复字符作相应的处理
    uniq -u ##显示唯一的行
    uniq -d ##显示重复的行
    uniq -c ##每行显示一次并统计重复次数
[root@localhost mnt]# vim tutu
[root@localhost mnt]# cat tutu
1
4
1
5
4
2
3
6
2
4
[root@localhost mnt]# sort tutu
1
1
2
2
3
4
4
4
5
6
[root@localhost mnt]# sort tutu -r
6
5
4
4
4
3
2
2
1
1
[root@localhost mnt]#
[root@localhost mnt]# vim tutu
[root@localhost mnt]# sort tutu
1
16
2
2
31
4
4
42
5
6
[root@localhost mnt]# sort tutu -n
1
2
2
4
4
5
6
16
31
42
[root@localhost mnt]# sort tutu -nr
42
31
16
6
5
4
4
2
2
1
[root@localhost mnt]# sort tutu -nru
42
31
16
6
5
4
2
1
[root@localhost mnt]# sort tutu -nru -o file
[root@localhost mnt]# cat file
42
31
16
6
5
4
2
1
[root@localhost mnt]#
[root@localhost mnt]# vim tutu
[root@localhost mnt]# sort tutu -n
1:1
2:3
2:4
4:2
4:9
5:3
6:1
16:4
31:5
42:8
[root@localhost mnt]# sort tutu -nr
42:8
31:5
16:4
6:1
5:3
4:9
4:2
2:4
2:3
1:1
[root@localhost mnt]# sort tutu -t ":" -k 1 -n
1:1
2:3
2:4
4:2
4:9
5:3
6:1
16:4
31:5
42:8
[root@localhost mnt]# sort tutu -t ":" -k 2 -n
1:1
6:1
4:2
2:3
5:3
16:4
2:4
31:5
42:8
4:9
[root@localhost mnt]# 
[root@localhost mnt]# vim tutu
[root@localhost mnt]# sort tutu -n
1
1
2
2
3
4
4
4
5
6
[root@localhost mnt]# sort tutu -n | uniq -u
3
5
6
[root@localhost mnt]# sort tutu -n | uniq -d
1
2
4
[root@localhost mnt]#sort tutu -n | uniq -c2 12 21 33 41 51 6
[root@localhost mnt]# 
  • && 和 ||

&& 表示前一条命令执行成功时,才执行后一条命令
方式:command1 && command2
如果command1执行成功,则执行command2
|| 表示上一条命令执行失败后,才执行下一条命令
方式:command1 || command2
如果command1执行失败,则执行command2

  • test
    test 命令与[] 等同
  • test "$A" == "$B" 等同于 [ "$A" == "$B" ]
linux中shell编程中的test常见用法:1. 判断表达式
if test                  #表达式为真
if test !                #表达式为假
test 表达式1 –a 表达式2    #两个表达式都为真
test 表达式1 –o 表达式2    #两个表达式有一个为真
test 表达式1 ! 表达式2     #条件求反2. 判断字符串
test –n 字符串            #字符串的长度非零
test –z 字符串            #字符串的长度是否为零
test 字符串1=字符串2       #字符串是否相等,若相等返回true
test 字符串1!=字符串2      #字符串是否不等,若不等返回false3. 判断整数
test 整数1 -eq 整数2       #整数相等
test 整数1 -ge 整数2       #整数1大于等于整数2
test 整数1 -gt 整数2       #整数1大于整数2
test 整数1 -le 整数2       #整数1小于等于整数2
test 整数1 -lt 整数2       #整数1小于整数2
test 整数1 -ne 整数2       #整数1不等于整数24. 判断文件(常用)
test File1 –ef File2    两个文件是否为同一个文件,可用于硬连接。主要判断两个文件是否指向同一个inode。
test File1 –nt File2    判断文件1是否比文件2新
test File1 –ot File2    判断文件1比是否文件2旧
test –e File   #文件是否存在
test –f File   #文件是否为正规(普通)文件
test –L File   #文件是否是一个符号链接(同-h)
test –S File   #文件是否是套接字
test –b file   #文件是否是块设备文件
test –d File   #文件是否是目录
test –c File   #文件是否是字符设备文件

判断文件

[root@localhost mnt]# ls
[root@localhost mnt]# touch file
[root@localhost mnt]# ln /mnt/file /mnt/file1
[root@localhost mnt]# ll
total 0
-rw-r--r--. 2 root root 0 Jun 12 05:17 file
-rw-r--r--. 2 root root 0 Jun 12 05:17 file1
[root@localhost mnt]# ls -li *
8844066 -rw-r--r--. 2 root root 0 Jun 12 05:17 file
8844066 -rw-r--r--. 2 root root 0 Jun 12 05:17 file1
[root@localhost mnt]# [ "/mnt/file" -ef "/mnt/file1" ]&& echo yes || echo no
yes
[root@localhost mnt]# [ "/mnt/file" -ef "/etc/passwd" ]&& echo yes || echo no
no
[root@localhost mnt]# rm -fr file1
[root@localhost mnt]# ll
total 0
-rw-r--r--. 1 root root 0 Jun 12 05:17 file
[root@localhost mnt]# touch file1
[root@localhost mnt]# ll
total 0
-rw-r--r--. 1 root root 0 Jun 12 05:17 file
-rw-r--r--. 1 root root 0 Jun 12 05:23 file1
[root@localhost mnt]# [ "file" -ot "file1" ]&& echo yes || echo no
yes
[root@localhost mnt]# [ "file" -nt "file1" ]&& echo yes || echo no
no
[root@localhost mnt]#
[root@localhost mnt]# vim file.sh
[root@localhost mnt]# cat file.sh
##################################
# Author:        tutu            #
# Version:                       #
# Mail:                          #
# Date:          2018-06-12      #
# Description:                   #
#                                #
###################################!/bin/bash
[ "$1" "/mnt/file" ] && echo yes || echo no
[root@localhost mnt]# ls
file.sh
[root@localhost mnt]# sh file.sh -e
no
[root@localhost mnt]# touch file
[root@localhost mnt]# sh file.sh -e
yes
[root@localhost mnt]# sh file.sh -f
yes
[root@localhost mnt]# sh file.sh -L
no
[root@localhost mnt]# ls
file  file.sh
[root@localhost mnt]# rm -fr file
[root@localhost mnt]# ln -s /mnt/file.sh /mnt/file
[root@localhost mnt]# ll
total 4
lrwxrwxrwx. 1 root root  12 Jun 12 05:33 file -> /mnt/file.sh
-rw-r--r--. 1 root root 339 Jun 12 05:31 file.sh
[root@localhost mnt]# sh file.sh -L
yes
[root@localhost mnt]# sh file.sh -S
no
[root@localhost mnt]# systemctl start mariadb
[root@localhost mnt]# rsync -D /var/lib/mysql/mysql.sock /mnt/file
[root@localhost mnt]# sh file.sh -S
yes
[root@localhost mnt]# ll
total 4
srwxrwxrwx. 1 root root   0 Jun 12 05:35 file
-rw-r--r--. 1 root root 339 Jun 12 05:31 file.sh
[root@localhost mnt]# sh file.sh -b
no
[root@localhost mnt]# rm -fr file
[root@localhost mnt]# rsync -D /dev/vdb /mnt/file
[root@localhost mnt]# sh file.sh -b
yes
[root@localhost mnt]# rm -fr file
[root@localhost mnt]# mkdir file
[root@localhost mnt]# sh file.sh -d
yes
[root@localhost mnt]# sh file.sh -c
no
[root@localhost mnt]# rm -fr file
[root@localhost mnt]# rsync -D /dev/pts/1 /mnt/file
[root@localhost mnt]# ll
total 4
crw-------. 1 root root 136, 1 Jun 12 05:37 file
-rw-r--r--. 1 root root    339 Jun 12 05:31 file.sh
[root@localhost mnt]# sh file.sh -c
yes
[root@localhost mnt]#

编写脚本——判断文件是否存在,存在的话,显示出该文件的类型

[root@localhost mnt]# vim file_check.sh
[root@localhost mnt]# cat file_check.sh
##################################
# Author:        tutu            #
# Version:                       #
# Mail:                          #
# Date:          2018-06-12      #
# Description:                   #
#                                #
###################################!/bin/bash
[ -z "$1" ]&&{echo "Please input a filename after script!!"exit 1
}
[ -e "$1" ]||{echo "$1 not exist!!"exit 0
}
[ -f "$1" ]&&{echo "$1 exists and is a regular file !!"exit 0
}
[ -d "$1" ]&&{echo "$1 exists and is a directory !!"exit 1
}
[root@localhost mnt]# sh file_check.sh
Please input a filename after script!!
[root@localhost mnt]# sh file_check.sh tutu
tutu not exist!!
[root@localhost mnt]# touch tutu
[root@localhost mnt]# sh file_check.sh tutu
tutu exists and is a regular file !!
[root@localhost mnt]# mkdir butter
[root@localhost mnt]# sh file_check.sh butter
butter exists and is a directory !!
[root@localhost mnt]#
  • tr

tr用来从标准输入中通过替换或删除操作进行字符转换。

字母大小写的转换

[root@localhost mnt]# vim tr.sh
[root@localhost mnt]# cat tr.sh
##################################
# Author:        tutu            #
# Version:                       #
# Mail:                          #
# Date:          2018-06-12      #
# Description:                   #
#                                #
###################################!/bin/bash
[ "$1" = "hello" ]&& {echo yes
}||{echo no
}
[root@localhost mnt]# sh tr.sh hello
yes
[root@localhost mnt]# sh tr.sh HELLO
no
[root@localhost mnt]# vim tr.sh
[root@localhost mnt]# cat tr.sh
##################################
# Author:        tutu            #
# Version:                       #
# Mail:                          #
# Date:          2018-06-12      #
# Description:                   #
#                                #
###################################!/bin/bash
WORD=$(echo $1 | tr 'A-Z' 'a-z')
[ "$WORD" = "hello" ]&& {echo yes
}||{echo no
}
[root@localhost mnt]# sh tr.sh hello
yes
[root@localhost mnt]# sh tr.sh HELLO
yes
[root@localhost mnt]#

linux——编写Shell脚本常用命令:diff、patch、cut、sort、uniq、、||、test、tr相关推荐

  1. linux脚本基本命令大全,Shell脚本常用命令

    Shell脚本常用命令 1           Shell中的特殊符号 1.1           $  美元符号.用来表示变量的值.如变量NAME的值为Mike,则使用$NAME就可以得到" ...

  2. linux常用的命令shell,Linux操作系统Shell和常用命令汇总

    简述 本文主要介绍Shell和Linux常用命令. Shell Shell 是一个用 C 语言编写的应用程序,提供了用户访问Linux操作系统内核服务的界面,它可以用来启动.挂起.停止.编写程序.Sh ...

  3. linux 命令脚本文件,Linux编写shell脚本执行多个命令

    背景:Linux做项目写完代码之后,需要用一个文件夹下多个文件进行测试,而且需要对于同一个文件执行多个命令,这个时候如果一个一个命令输入比较繁琐,于是写了一个简单的命令脚本如下: #! /bin/ba ...

  4. linux 脚本监听,Linux—编写shell脚本监控主机

    编写SHELL脚本监控主机 1.用vi命令在/root目录是新建一个脚本文件sysmon.sh: 内容如下: #!/bin/bash dug=$(df -h | grep "/$" ...

  5. Linux学习入门: shell脚本常用命令汇总

    提示:文章写完后,目录可以自动生成,如何生成可参考右边的帮助文档 前言 Linux学习最初就是要掌握shell脚本的基本命令语句,这个也是我们操作Linux操作系统的关键步骤,但是Linux的shel ...

  6. ubuntu及shell脚本常用命令入门

    一.Shell命令 二.ubuntu软件安装方法 三.Ubuntu文件系统结构 四.ubuntu磁盘文件 五.Linux下常用的压缩格式 六.linux用户和用户组 七.Ubuntu/Linux文件权 ...

  7. linux 编写shell脚本

    目录 简单shell 示例 接受用户参数 判断用户参数(判断语法) 流程控制语句 (if .for .while.case) 简单shell 示例 Shell脚本命令的工作方式有两种:交互式和批处理. ...

  8. Linux—编写shell脚本操作数据库执行sql

    修改数据库数据   在升级应用时,我们常常会遇到升级数据库的问题,这就涉及到sql脚本的编写.   一般我们会通过写sql脚本,然后将xxx.sql脚本放到数据库中进行source xxx.sql执行 ...

  9. 【linux】shell脚本 ps 命令学习

    目录 1. ps 常用的命令 2. ps -ef 命令扩展使用 3. ps 命令的简单应用 Linux ps (英文全拼:process status)命令用于显示当前进程的状态,类似于 window ...

最新文章

  1. C++知识点总结(纯C++!!)
  2. LINUX设备驱动之设备模型一--kobject
  3. java static null,我们可以在Java中使用null对象调用静态方法吗?如果是这样,怎么样?...
  4. 统一建模语言(UML) 版本 2.0
  5. Java 阶段面试 知识点合集 - 我们到底能走多远系列(15)
  6. 面试中海量数据处理总结
  7. Zend Framework实例教程三
  8. 江苏省计算机一级题库软件百度云,江苏省计算机一级B题库11
  9. 自考本科计算机类专业查询,自考本科学位如何查询
  10. @ModelAttribute注解使用
  11. Spring Boot中初始化资源的几种方式
  12. php中$t=date()函数参数意义及时间更改
  13. bwt比对算法 C语言,BWT比对算法
  14. 第一章,实现数据完整性
  15. 平均风向计算中对于风向角的判断
  16. java大作业设计_Java程序设计_大作业.doc
  17. python+Django搭建web服务器
  18. 0011基于单片机电子密码锁控制系统设计
  19. 计算机应用技术办公室自动化,办公室自动化的计算机处理技术应用研究
  20. python 多图绘制

热门文章

  1. 【学习笔记】HTTPS概述
  2. 致远表单代办状态删除
  3. 非库存采购的自动记帐
  4. 关于未达账项的账务处理
  5. 凭证 90000000 保存(帐户确定出错)
  6. SAP Hybris电子商务最新功能
  7. CLASS ALV Event
  8. 上市公司总市值TOP10出炉,你所在县区看的到未来吗?
  9. 人脸识别与膜虹识别_当人脸识别遭遇口罩,虹膜识别的机会来了
  10. 分类的评估标准_机器学习:模型评估之评估指标