Linux--Shell腳本常用命令

diff

比較兩個文件或目錄的不同

diff test1 test2

在這裏插入圖片描述

diff test1 test2

test2 文件 test 後面有一個空格

在這裏插入圖片描述

-b
不檢查空格字符不同

diff -b test1 test2

在這裏插入圖片描述

-B
不檢查空白行

diff test1 test2

diff -B test1 test2

test2 文件必比 test1 文件多了一行空行

在這裏插入圖片描述

-c
顯示全部內容,並標出不同之處

diff -c test1 test2

test1 與 test2 時間戳不同

test2 多了 一行內容:123

在這裏插入圖片描述

-i
不檢查大小寫的不同

diff test1 test2

test1 內容 test

test2 內容第一行:tEst

第二行:123

diff -i test1 test2

只顯示 test2 不同,多了一行 123

忽略掉檢查大小寫

在這裏插入圖片描述

-q
僅顯示有無差異,不顯示詳細的信息

diff test1 test2

diff -q test1 test2

提示文件 test1 與 test2 有不同之處,不顯示不同之處

在這裏插入圖片描述

-r
比較子目錄中的文件

ls /d1 /d2

/d1/test1

/d2/test1

diff -r /d1/test1 /d2/test1

/d2/test1 比 /d1/test1 多了一行內容:123

/d2/test1 是大寫 S

/d1/test1 是小寫 s

在這裏插入圖片描述

-u
以合併的方式來顯示文件內容的不同

diff -u test1 test2

在這裏插入圖片描述

diff -u test1 test2 > test.path

test1 與 test2 不同之處,導入到 test.path (補丁)文件中

在這裏插入圖片描述

patch

用於不同文件之間打補丁

patch -b

yum install patch -y

在這裏插入圖片描述

patch -b test1 test.path

給 test1 文件打 test.path 補丁 使 test1 文件與 test2 文件相同

cat test1 test2

在這裏插入圖片描述

cut

多用於字符的截取

cut -d

指定分隔符

cut -f

指定截取的列

cut -c

指定截取的字符位置

實驗

截取 ifconfig eth0 命令顯示中的 ip :“172.25.254.330”

ifconfig eth0

在這裏插入圖片描述

ifconfig eth0 | head -n 2 | tail -n 1 | cut -c 14-28

頭部2行 | 尾部一行 | 截取第 14 至 28 字符

172.25.254.230

在這裏插入圖片描述

截取 /etc/passwd 文件中第二行的 “nologin”

在這裏插入圖片描述

cat passwd | head -n 2 | tail -n 1 | cut -d : -f 7 | cut -d / -f 3

頭部 2 行 | 尾部 1 行 | 截取以:分隔的第七列中的以 / 分隔的第三列

在這裏插入圖片描述

head -n 2 | tail -n 1 | cut -d : -f 1,3,5

頭部 2 行 | 尾部 1行 | 分別截取以 : 分隔的第一、三、五列

在這裏插入圖片描述

sort

用於字符排序

在這裏插入圖片描述

-n
純數字排序
sort -n test
在這裏插入圖片描述

-r
倒序
sort -r test
在這裏插入圖片描述

sort -rn test
在這裏插入圖片描述

-u
去掉重複數字
sort -u test
在這裏插入圖片描述

sort -nru test
在這裏插入圖片描述

-o
輸出到指定文件中

-t
指定分隔符

-k
指定要排序的列

sort -nr test | uniq -c | sort -t " " -k 2

指定每行顯示一次並且按照分隔符空格排序兩列

在這裏插入圖片描述

sort -nr test | uniq -c | sort -t " " -k 2 -r

指定每行顯示一次並且按照分隔符空格排序兩列並且倒序排列

在這裏插入圖片描述

sort -nr test | uniq -c | sort -t " " -k 2 -r | head -1

指定每行顯示一次並且按照分隔符空格排序兩列並且倒序排列抓取第一行

在這裏插入圖片描述

sort -nr test | uniq -c | sort -t " " -k 2 -r -o test1

指定每行顯示一次並且按照分隔符空格排序兩列並且倒序排列輸出到 test1 文件內

cat test1

在這裏插入圖片描述

uniq

對重複字符做相應處理

在這裏插入圖片描述

-u
顯示唯一的行
uniq -u test
在這裏插入圖片描述

-d
顯示重複的行
uniq -d test

在這裏插入圖片描述

-c
每行顯示一次並統計重複次數
uniq -c test
在這裏插入圖片描述

&& 和 ||

&&用來執行條件成立後執行的命令

||用來執行條件不成立後執行的命令

實驗

vim /ping.sh

ping -w1 -c1 $1 > /dev/null && echo "$1 is up" || echo "$1 is down"

在這裏插入圖片描述

在這裏插入圖片描述

sh /ping.sh 172.25.254.30

網絡可達:172.25.254.30 is up

sh /ping.sh 192.168.0.30

網絡不可達:192.168.0.30 is down

在這裏插入圖片描述

test

test[] 等同

test "$A" == "$B" 等同於 [ "$A" == "$B" ]

[ "$A" = "$B" ]

A等於B

在這裏插入圖片描述

[ "$A" -eq "$B" ]

A等於B

在這裏插入圖片描述

[ "$A" != "$B" ]

A不等於B

在這裏插入圖片描述

[ "$A" -ne "$B" ]

A不等於B

在這裏插入圖片描述

[ "$A" -ge "$B" ]

A大於等於B

在這裏插入圖片描述

[ "$A" -gt "$B" ]

A大於B

在這裏插入圖片描述

[ "$A" -le "$B" ]

A小於等於B

在這裏插入圖片描述

[ "$A" -lt "$B" ]

A小於B

在這裏插入圖片描述

[ -z "$A" ]

字符串的長度爲空

在這裏插入圖片描述

[ -n "$A" ]

字符串的長度非空

在這裏插入圖片描述

test File1 –ef File2

兩個文件是否爲同一個文件,可用於硬連接,主要判斷兩個文件是否指向同一個inod

在這裏插入圖片描述

在這裏插入圖片描述

在這裏插入圖片描述

在這裏插入圖片描述

test File1 –nt File2

判斷文件1是否比文件2新

test File1 –ot File2

判斷文件1比是否文件2舊

在這裏插入圖片描述

Tr

轉換大小寫

在這裏插入圖片描述

實驗

查看 / cpu% 使用率

超過 30% 報警

報警信息導入到日誌

df | head -n 2 | tsil -n 1

在這裏插入圖片描述

vim logger.sh

sh logger.sh

cat /var/log/messages | tail -n 1

在這裏插入圖片描述

每分鐘監視一次 / cpu% 使用率

vim logger.sh

cat logger.sh

sh logger.sh

執行 job 任務

cat /var/log/messages | tail -n 1

在這裏插入圖片描述

實驗

vim /root/check_file.sh

[ -z “$1” ] && {
echo “Please input your filename”
exit
}
[ -e “$1” ] || {
echo “$1 is not exist”
exit
}
[ -f “$1” ] && {
echo “$1 is file”
exit
}
[ -d “$1” ] && {
echo “$1 is directory”
exit
}
[ -L"$1" ] && {
echo “$1 is softlinks”
exit
}
[ -S “$1” ] && {
echo “$1 is socket”
exit
}
[ -b “$1” ] && {
echo “$1 is block dev”
exit
}
[-c “$1” ] && {
echo “$1 is character dev”
exit
}

cat /root/check_file.sh

在這裏插入圖片描述

在這裏插入圖片描述

在這裏插入圖片描述

測試

在這裏插入圖片描述
在這裏插入圖片描述

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章