编写Shell脚本常用命令:diff、patch、cut、sort、uniq、&&、||、test、tr

diff 和 patch

diff命令在最简单的情况下,比较给定的两个文件的不同。如果使用“-”代替“文件”参数,则要比较的内容将来自标准输入。diff命令是以逐行的方式,比较文本文件的异同处。如果该命令指定进行目录的比较,则将会比较该目录中具有相同文件名的文件,而不会对其子目录文件进行任何比较操作。
patch 命令用于打补丁,补丁文件是使用diff产生的,
patch 命令失败或拒绝接受补丁时,会产生一个和原文件同名,以”.rej”为后缀的差异文件。
   当知道 -b 时,会产生一个和原文件同名,以”.orig”为后缀的备份文件

Diff命令
Diff命令是用来比较两个文件或目录的不同
Diff [options] target1 target2
Diff file1 file2
Diff direcory1 directory2
Diff 在比较文件过程中结果读取方式
[num1, num2]
比较两文件

[root@shell mnt]# vim westos
[root@shell mnt]# cat westos
hello qwe
[root@shell mnt]# vim westos1
[root@shell mnt]# cat westos1
hello qwe
hello
[root@shell mnt]# diff westos westos1
1a2                            westos第二行 添加hello为westos1
> hello
[root@shell mnt]# diff westos1 westos
2d1                            westos1删除第二行hello为 westos
< hello
[root@shell mnt]# vim westos
[root@shell mnt]# cat westos
hello qwe
123
[root@shell mnt]# cat westos1
hello qwe
hello
[root@shell mnt]# diff westos westos1     westos 和westos1相比,第二行内容不同
2c2
< 123                       westos为123
---
> hello                     westos1为hello
[root@shell mnt]# diff westos1 westos
2c2
< hello
---
> 123
两目录比较

[root@shell mnt]# mkdir linux
[root@shell mnt]# mkdir unix
[root@shell mnt]# touch linux/westos
[root@shell mnt]# diff -r linux/ unix/
Only in linux/: westos                只有linux目录下有/westos
[root@shell mnt]# diff -r unix/ linux/
Only in linux/: westos
[root@shell mnt]# touch unix/hehe
[root@shell mnt]# diff -r linux/ unix/
Only in unix/: hehe
Only in linux/: westos
[root@shell mnt]# ls
linux  unix  westos  westos1
[root@shell mnt]# diff westos westos1
2c2
< 123
---
> hello
[root@shell mnt]# diff -u westos westos1         ## -u —— 以合并的方式来显示文件内容的不同
--- westos  2018-06-09 22:47:11.010634166 -0400
+++ westos1 2018-06-09 22:44:21.512039194 -0400
@@ -1,2 +1,2 @@
 hello qwe
-123
+hello
[root@shell mnt]# diff -u westos westos1 > westos.path##把用合并的方式显示的文件内容的不同导出到文件 westos.path
[root@shell mnt]# cat westos.path 
--- westos  2018-06-09 22:47:11.010634166 -0400
+++ westos1 2018-06-09 22:44:21.512039194 -0400
@@ -1,2 +1,2 @@
 hello qwe
-123
+hello

打补丁

[root@shell mnt]# yum install patch -y           安装补丁文件
[root@shell mnt]# cat westos
hello qwe
123
[root@shell mnt]# cat westos1
hello qwe
hello
[root@shell mnt]# cat westos.path 
--- westos  2018-06-09 22:47:11.010634166 -0400
+++ westos1 2018-06-09 22:44:21.512039194 -0400
@@ -1,2 +1,2 @@
 hello qwe
-123
+hello
[root@shell mnt]# patch westos westos.path       给westos打补丁,不保存原文件
patching file westos
[root@shell mnt]# ls
linux  unix  westos  westos1  westos.path
[root@shell mnt]# cat westos
hello qwe
hello
[root@shell mnt]# cat westos1
hello qwe
hello
[root@shell mnt]# 
[root@shell mnt]# vim westos
[root@shell mnt]# cat westos
hello qwe
123
[root@shell mnt]# vim westos1
[root@shell mnt]# cat westos1
hello qwe
hello
[root@shell mnt]# ls
linux  unix  westos  westos1  westos.path
[root@shell mnt]# patch -b westos westos.path       给westos打补丁,保存原文件为westos.orig
patching file westos
[root@shell mnt]# ls
linux  unix  westos  westos1  westos.orig  westos.path
[root@shell mnt]# cat westos
hello qwe
hello
[root@shell mnt]# cat westos1
hello qwe
hello
[root@shell mnt]# cat westos.orig 
hello qwe
123

cut

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

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

命令和脚本实现查看 IP

显示当前ip
[root@shell mnt]# vim ip_show.sh
[root@shell mnt]# sh ip_show.sh 
172.25.254.136
[root@shell mnt]# cat ip_show.sh 
############################
# Author:     bo           #
# Version:             #
# Mail:                #
# Date:   2018-06-09       #
# Description:             #
#                  #
############################

#!/bin/bash
ifconfig eth0 | awk -F " " '/inet\>/{print $2}'

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@shell mnt]# vim westos
[root@shell mnt]# cat westos 
3
6
8
1
5
2
3
1
9
6

[root@shell mnt]# sort westos       排列

1
1
2
3
3
5
6
6
8
9
[root@shell mnt]# vim westos
[root@shell mnt]# cat westos 
3
6
8
1
5
2
3
1
9
6
23
10
53
11
[root@shell mnt]# sort westos      
1
1
10
11
2
23
3
3
5
53
6
6
8
9
[root@shell mnt]# sort -n westos            纯数字排列
1
1
2
3
3
5
6
6
8
9
10
11
23
53
[root@shell mnt]# sort -rn westos         纯数字倒序排列
53
23
11
10
9
8
6
6
5
3
3
2
1
1
[root@shell mnt]# sort -n -u westos       纯数字倒序排列且去掉重复的
1
2
3
5
6
8
9
10
11
23
53
[root@shell mnt]# sort -rnu westos -o file     纯数字倒序排列且去掉重复的并将结果保存在file中
[root@shell mnt]# cat file 
53
23
11
10
9
8
6
5
3
2
1
[root@shell mnt]# sort -rnu westos
53
23
11
10
9
8
6
5
3
2
1
[root@shell mnt]# vim westos
[root@shell mnt]# cat westos 
3:1
6:5
8:6
1:4
5:9
2:2
3:8
1:2
9:9
6:25
23:12
10:6
53:02
11:2
[root@shell mnt]# sort -t : -k 2 westos 
53:02
3:1
23:12
11:2
1:2
2:2
6:25
1:4
6:5
10:6
8:6
3:8
5:9
9:9
[root@shell mnt]# sort -t : -nk 2 westos 
3:1
11:2
1:2
2:2
53:02
1:4
6:5
10:6
8:6
3:8
5:9
9:9
23:12
6:25


[root@shell mnt]# vim westos 
[root@shell mnt]# cat westos 
1
2
6
3
8
1
9
1
5
2
9
1
3

[root@shell mnt]# sort -n westos | uniq -c     每行显示一次并统计显示个数
      1 
      4 1
      2 2
      2 3
      1 5
      1 6
      1 8
      2 9
[root@shell mnt]# sort -n westos | uniq -d  显示重复的
1
2
3
9
[root@shell mnt]# sort -n westos | uniq -u          显示不重复的

5
6
8

&& 和 ||

&& 表示前一条命令执行成功时,才执行后一条命令
方式: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      #字符串是否不等,若不等返回false
 3. 判断整数
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不等于整数2
 4. 判断文件(常用)
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@shell mnt]# ls
check_ip.sh  file  ip_show.sh  laozizuida  num_check.sh  westos
[root@shell mnt]# touch file
[root@shell mnt]# ln /mnt/file /mnt/file1
[root@shell mnt]# ls -l
total 24
-rw-r--r-- 1 root root 284 Jun 10 01:38 check_ip.sh
-rw-r--r-- 2 root root  26 Jun 10 02:54 file
-rw-r--r-- 2 root root  26 Jun 10 02:54 file1
-rw-r--r-- 1 root root 268 Jun  9 23:55 ip_show.sh
drwxr-xr-x 2 root root  47 Jun 10 02:04 laozizuida
-rw-r--r-- 1 root root 399 Jun 10 02:51 num_check.sh
-rw-r--r-- 1 root root  27 Jun 10 01:51 westos
[root@shell mnt]# [ "/mnt/file" -ef "/mnt/file1" ]&& echo yes || echo no    两文件是否字节一致
yes
[root@shell mnt]# [ "/mnt/file" -ef "/mnt/ip_show.sh" ]&& echo yes || echo no
no
[root@shell mnt]# rm -rf file1
[root@shell mnt]# touch file1
[root@shell mnt]# [ "/mnt/file" -ot "/mnt/file1" ]&& echo yes || echo no  file比file1老  对     
yes
[root@shell mnt]# [ "/mnt/file" -nt "/mnt/file1" ]&& echo yes || echo no  file比file1新  错    
no

[root@shell mnt]# vim file.sh
[root@shell mnt]# cat file.sh 
############################
# Author:     bo           #
# Version:             #
# Mail:                #
# Date:   2018-06-10       #
# Description:             #
#                  #
############################

#!/bin/bash
[ "$1" "/mnt/file" ] && echo yes || echo no





[root@shell mnt]# ls
file.sh
[root@shell mnt]# sh file.sh -e
no
[root@shell mnt]# touch file
[root@shell mnt]# sh file.sh -e
yes
[root@shell mnt]# sh file.sh -f
yes
[root@shell mnt]# sh file.sh -L
no
[root@shell mnt]# ls
file  file.sh
[root@shell mnt]# rm -fr file
[root@shell mnt]# ln -s /mnt/file.sh /mnt/file
[root@shell 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@shell mnt]# sh file.sh -L
yes
[root@shell mnt]# sh file.sh -S
no
[root@shell mnt]# systemctl start mariadb
[root@shell mnt]# rsync -D /var/lib/mysql/mysql.sock /mnt/file
[root@shell mnt]# sh file.sh -S
yes
[root@shell 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@shell mnt]# sh file.sh -b
no
[root@shell mnt]# rm -fr file
[root@shell mnt]# rsync -D /dev/vdb /mnt/file
[root@shell mnt]# sh file.sh -b
yes
[root@shell mnt]# rm -fr file
[root@shell mnt]# mkdir file
[root@shell mnt]# sh file.sh -d
yes
[root@shell mnt]# sh file.sh -c
no
[root@shell mnt]# rm -fr file
[root@shell mnt]# rsync -D /dev/pts/1 /mnt/file
[root@shell 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@shell mnt]# sh file.sh -c
yes
Tr  转换大小写

[root@shell mnt]# vim test.sh
[root@shell mnt]# cat test.sh 
############################
# Author:     bo           #
# Version:                 #
# Mail:                    #
# Date:   2018-06-14       #
# Description:             #
#                          #
############################

#!/bin/bash
WORD=$(echo $1 | tr 'A-Z' 'a-z' )
[ "$WORD" = "hello" ]&& {
    echo yes
}||{
    echo no
}
[root@shell mnt]# sh test.sh hello
yes
[root@shell mnt]# sh test.sh HELLO
yes

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

[root@shell mnt]# vim file_check.sh 
[root@shell 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@shell mnt]# sh file_check.sh 
Please input a filename after script!!
[root@shell mnt]# sh file_check.sh hello
hello not exist!!
[root@shell mnt]# touch tutu
[root@shell mnt]# sh file_check.sh hello
hello exists and is a regular file !!
[root@shell mnt]# mkdir butter
[root@shell mnt]# sh file_check.sh butter
butter exists and is a directory !!
[root@shell mnt]#
判断输入数字是否属于110
[root@shell mnt]# vim num_check.sh
[root@shell mnt]# cat num_check.sh 
############################
# Author:     bo           #
# Version:             #
# Mail:                #
# Date:   2018-06-10       #
# Description:             #
#                  #
############################

#!/bin/bash
[ -z "$1" ] && {
    echo please input a nmuber after script!!!
    exit 1
}
[ "$1" -gt "0" -a "$1" -lt "10" ]  && {
    echo "$1 is between 1 ~ 10"
}||{
    echo "$1 is notbetween 1 ~ 10"
}
[root@shell mnt]# sh num_check.sh
please input a nmuber after script!!!
[root@shell mnt]# sh num_check.sh 1
1 is between 1 ~ 10
[root@shell mnt]# sh num_check.sh 11
11 is notbetween 1 ~ 10

猜你喜欢

转载自blog.csdn.net/awoyaoc/article/details/80699187