日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 > 运维知识 > linux >内容正文

linux

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

發(fā)布時間:2025/3/19 linux 44 豆豆
生活随笔 收集整理的這篇文章主要介紹了 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”為后綴的備份文件。

[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行內(nèi)容 > I like tutu [root@localhost mnt]# diff tutu1 tutu 2d1 ##表示文件 tutu1 與文件t utu 相比,多了第2行內(nèi)容 < 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 相比,第二行內(nèi)容有所不同 < butterbly --- > I like tutu [root@localhost mnt]# diff -u tutu tutu1 ## -u —— 以合并的方式來顯示文件內(nèi)容的不同。 --- 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 ##把用合并的方式顯示的文件內(nèi)容的不同導(dǎo)出到文件 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 —— 字節(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ù)
[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]#
  • && 和 ||

&& 表示前一條命令執(zhí)行成功時,才執(zhí)行后一條命令
方式:command1 && command2
如果command1執(zhí)行成功,則執(zhí)行command2
|| 表示上一條命令執(zhí)行失敗后,才執(zhí)行下一條命令
方式:command1 || command2
如果command1執(zhí)行失敗,則執(zhí)行command2

  • test
    test 命令與[] 等同
  • test "$A" == "$B" 等同于 [ "$A" == "$B" ]
linux中shell編程中的test常見用法:1. 判斷表達(dá)式 if test #表達(dá)式為真 if test ! #表達(dá)式為假 test 表達(dá)式1 –a 表達(dá)式2 #兩個表達(dá)式都為真 test 表達(dá)式1 –o 表達(dá)式2 #兩個表達(dá)式有一個為真 test 表達(dá)式1 ! 表達(dá)式2 #條件求反2. 判斷字符串 test –n 字符串 #字符串的長度非零 test –z 字符串 #字符串的長度是否為零 test 字符串1=字符串2 #字符串是否相等,若相等返回true test 字符串1!=字符串2 #字符串是否不等,若不等返回false3. 判斷整數(shù) test 整數(shù)1 -eq 整數(shù)2 #整數(shù)相等 test 整數(shù)1 -ge 整數(shù)2 #整數(shù)1大于等于整數(shù)2 test 整數(shù)1 -gt 整數(shù)2 #整數(shù)1大于整數(shù)2 test 整數(shù)1 -le 整數(shù)2 #整數(shù)1小于等于整數(shù)2 test 整數(shù)1 -lt 整數(shù)2 #整數(shù)1小于整數(shù)2 test 整數(shù)1 -ne 整數(shù)2 #整數(shù)1不等于整數(shù)24. 判斷文件(常用) test File1 –ef File2 兩個文件是否為同一個文件,可用于硬連接。主要判斷兩個文件是否指向同一個inode。 test File1 –nt File2 判斷文件1是否比文件2新 test File1 –ot File2 判斷文件1比是否文件2舊 test –e File #文件是否存在 test –f File #文件是否為正規(guī)(普通)文件 test –L File #文件是否是一個符號鏈接(同-h) test –S File #文件是否是套接字 test –b file #文件是否是塊設(shè)備文件 test –d File #文件是否是目錄 test –c File #文件是否是字符設(shè)備文件

判斷文件

[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)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。

主站蜘蛛池模板: 亚洲黑丝在线 | 久久久黄色| 极品色av| 成人国产片女人爽到高潮 | 日日干av| 操出白浆视频 | 日韩爱爱爱 | 亚洲一区在线看 | 丁香九月激情 | 久久久久久久久综合 | 亚洲熟区| 懂色av蜜臀av粉嫩av分 | 欧美日韩国产91 | 婷婷看片| fc2ppv色の美マンに中出し | 国产白丝在线观看 | 中文字幕一区二区三区精华液 | 中文字幕视频一区二区 | 国语粗话呻吟对白对白 | 四虎国产精品永久免费观看视频 | 九九热在线视频 | 在线中文字幕av | 一级黄色免费看 | 成人国产在线视频 | 色盈盈影院 | 日本不卡视频一区二区三区 | 91av色| 91视频一区二区 | 亚洲激情av | 成人免费看av | 亚洲黄色成人网 | 手机看片在线观看 | 污视频网址在线观看 | 久操综合| 色噜噜狠狠一区二区三区牛牛影视 | a在线天堂 | 8x8ⅹ国产精品一区二区二区 | 少妇欧美激情一区二区三区 | 日本欧美一区二区三区不卡视频 | 国产吞精囗交免费视频网站 | 五月av在线| wwwxxx黄色 | 在线中文字幕视频 | 久久久久国产 | 加勒比一区二区 | 欧美黄色aaa | 私人影院毛片 | 欧美精品一卡 | 久久久久国产精品无码免费看 | 亚洲高清视频一区二区 | 老司机午夜精品视频 | 国产网站黄色 | 香蕉视频在线免费看 | 成年人免费高清视频 | 天天操天天插 | 性欧美ⅹxxxx极品护士 | 蜜臀久久99精品久久久久宅男 | 一区二区三区免费高清视频 | 久久色网 | 五月综合色 | 久久久久久久久久久久久久久久久久久 | 欧美激情喷水 | 嫩草影院黄 | 国产美女又黄又爽又色视频免费 | 日本高清一区二区视频 | 国产精品野外户外 | 91夫妻在线| 天天躁狠狠躁狠狠躁夜夜躁68 | 欧美视频久久久 | 在线看的免费网站 | 亚洲欧美另类视频 | 少妇人妻偷人精品无码视频新浪 | 婷婷色六月 | 欧美图片一区二区 | 操一操 | 香蕉影音| 男人舔女人下部高潮全视频 | 国产一级免费大片 | 国产三级按摩推拿按摩 | 高清av一区二区 | 亚洲av无码专区在线电影 | 色一区二区| 国产少女免费观看高清 | 丰满少妇高潮在线观看 | 亚洲专区第一页 | 美女裸片 | 色偷偷av| 成人手机在线免费视频 | 精品国产乱码久久久久久郑州公司 | 国产一级做a爰片久久毛片男男 | 欧美精品1区 | 亚洲三级欧美 | 日本老师巨大bbw丰满 | 啊v视频在线观看 | 国产原创在线观看 | 日日干,夜夜操 | 日韩中文字幕在线免费观看 | 成年人网站在线观看视频 | 一区二区三区人妻 |