linux——编写Shell脚本常用命令:diff、patch、cut、sort、uniq、、||、test、tr
生活随笔
收集整理的這篇文章主要介紹了
linux——编写Shell脚本常用命令:diff、patch、cut、sort、uniq、、||、test、tr
小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.
- diff 和 patch
命令幫助:diff –help | patch –help
- diff命令在最簡單的情況下,比較給定的兩個文件的不同。如果使用“-”代替“文件”參數(shù),則要比較的內(nèi)容將來自標(biāo)準(zhǔn)輸入。diff命令是以逐行的方式,比較文本文件的異同處。如果該命令指定進(jìn)行目錄的比較,則將會比較該目錄中具有相同文件名的文件,而不會對其子目錄文件進(jìn)行任何比較操作。
- patch 命令用于打補丁,補丁文件是使用diff產(chǎn)生的,
patch 命令失敗或拒絕接受補丁時,會產(chǎn)生一個和原文件同名,以”.rej”為后綴的差異文件。
當(dāng)知道 -b 時,會產(chǎn)生一個和原文件同名,以”.orig”為后綴的備份文件。
- diff命令在最簡單的情況下,比較給定的兩個文件的不同。如果使用“-”代替“文件”參數(shù),則要比較的內(nèi)容將來自標(biāo)準(zhǔn)輸入。diff命令是以逐行的方式,比較文本文件的異同處。如果該命令指定進(jìn)行目錄的比較,則將會比較該目錄中具有相同文件名的文件,而不會對其子目錄文件進(jìn)行任何比較操作。
- [root@localhost mnt]# yum install patch -y安裝補丁工具
- cut
cut命令可以從一個文本文件或者文本流中提取文本列。
命令用法:
- cut -b filename
- cut -c filename
- cut -f filename
- -b —— 字節(jié)、-c —— 字符、-f —— 字段
命令和腳本實現(xiàn)查看 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 ##腳本實現(xiàn) [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里非常有用,它將文件進(jìn)行排序,并將排序結(jié)果標(biāo)準(zhǔn)輸出。
- sort
多用于字符排序
sort -n ##純數(shù)字排序
sort -r ##倒序
sort -u ##去掉重復(fù)數(shù)字
sort -o ##輸出到指定文件中
sort -t ##指定分隔符
sort -k ##指定要排序的列
uniq命令用于報告或忽略文件中的重復(fù)行,一般與sort命令結(jié)合使用
- uniq
對重復(fù)字符作相應(yīng)的處理
uniq -u ##顯示唯一的行
uniq -d ##顯示重復(fù)的行
uniq -c ##每行顯示一次并統(tǒng)計重復(fù)次數(shù)
- && 和 ||
&& 表示前一條命令執(zhí)行成功時,才執(zhí)行后一條命令
方式:command1 && command2
如果command1執(zhí)行成功,則執(zhí)行command2
|| 表示上一條命令執(zhí)行失敗后,才執(zhí)行下一條命令
方式:command1 || command2
如果command1執(zhí)行失敗,則執(zhí)行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用來從標(biāo)準(zhǔn)輸入中通過替換或刪除操作進(jìn)行字符轉(zhuǎn)換。
字母大小寫的轉(zhuǎn)換
[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]#總結(jié)
以上是生活随笔為你收集整理的linux——编写Shell脚本常用命令:diff、patch、cut、sort、uniq、、||、test、tr的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: linux——Shell脚本说明、创建、
- 下一篇: linux——grep、sed、awk整