linux——编写Shell脚本常用命令:diff、patch、cut、sort、uniq、、||、test、tr
生活随笔
收集整理的这篇文章主要介绍了
linux——编写Shell脚本常用命令:diff、patch、cut、sort、uniq、、||、test、tr
小编觉得挺不错的,现在分享给大家,帮大家做个参考.
- diff 和 patch
命令帮助:diff –help | patch –help
- diff命令在最简单的情况下,比较给定的两个文件的不同。如果使用“-”代替“文件”参数,则要比较的内容将来自标准输入。diff命令是以逐行的方式,比较文本文件的异同处。如果该命令指定进行目录的比较,则将会比较该目录中具有相同文件名的文件,而不会对其子目录文件进行任何比较操作。
- patch 命令用于打补丁,补丁文件是使用diff产生的,
patch 命令失败或拒绝接受补丁时,会产生一个和原文件同名,以”.rej”为后缀的差异文件。
当知道 -b 时,会产生一个和原文件同名,以”.orig”为后缀的备份文件。
- diff命令在最简单的情况下,比较给定的两个文件的不同。如果使用“-”代替“文件”参数,则要比较的内容将来自标准输入。diff命令是以逐行的方式,比较文本文件的异同处。如果该命令指定进行目录的比较,则将会比较该目录中具有相同文件名的文件,而不会对其子目录文件进行任何比较操作。
- [root@localhost mnt]# yum install patch -y安装补丁工具
- 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 ##每行显示一次并统计重复次数
- && 和 ||
&& 表示前一条命令执行成功时,才执行后一条命令
方式:command1 && command2
如果command1执行成功,则执行command2
|| 表示上一条命令执行失败后,才执行下一条命令
方式:command1 || command2
如果command1执行失败,则执行command2
- test
test 命令与[] 等同 - test "$A" == "$B" 等同于 [ "$A" == "$B" ]
判断文件
[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的全部内容,希望文章能够帮你解决所遇到的问题。
- 上一篇: linux——Shell脚本说明、创建、
- 下一篇: linux——grep、sed、awk整