欢迎访问 生活随笔!

生活随笔

当前位置: 首页 > 运维知识 > linux >内容正文

linux

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

发布时间:2025/3/19 linux 60 豆豆
生活随笔 收集整理的这篇文章主要介绍了 linux——编写Shell脚本常用命令:diff、patch、cut、sort、uniq、、||、test、tr 小编觉得挺不错的,现在分享给大家,帮大家做个参考.
  • 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的全部内容,希望文章能够帮你解决所遇到的问题。

如果觉得生活随笔网站内容还不错,欢迎将生活随笔推荐给好友。