ls
創(chuàng)新互聯(lián)建站專注服務(wù)器托管服務(wù)十年,提供服務(wù)器租用、虛擬主機(jī)、域名注冊、云服務(wù)器、云主機(jī)租用、雙線服務(wù)器托管、建站等服務(wù)
#列出以a和o開頭的所有文件
[root@sh02-hap-bss-prod-consul03 ~]# ls
anaconda-ks.cfg nss-pam-ldapd-0.9.8-1.gf.el7.x86_64.rpm openldap-clients-2.4.44-21.el7_6.x86_64.rpm original-ks.cfg tools
[root@sh02-hap-bss-prod-consul03 ~]# ls [ao]*
anaconda-ks.cfg openldap-clients-2.4.44-21.el7_6.x86_64.rpm original-ks.cfg
#[0-9]表示任意單個數(shù)字
#[!0-9]表示非數(shù)字開頭的字符串
[root@sh02-hap-bss-prod-consul03 ~]# ls
1 3 anaconda-ks.cfg openldap-clients-2.4.44-21.el7_6.x86_64.rpm tools
2 44 nss-pam-ldapd-0.9.8-1.gf.el7.x86_64.rpm original-ks.cfg
[root@sh02-hap-bss-prod-consul03 ~]# ls [!0-9]*
anaconda-ks.cfg nss-pam-ldapd-0.9.8-1.gf.el7.x86_64.rpm openldap-clients-2.4.44-21.el7_6.x86_64.rpm original-ks.cfg
tools:
libnss-cache nsscache
[root@sh02-hap-bss-prod-consul03 ~]# ls [0-9]*
1 2 3 44
rm
#刪除以數(shù)字開頭的文件
rm -f [0-9]*
#刪除非以數(shù)字開頭的文件
[root@sh02-hap-bss-prod-consul03 test]# ls
1 2 3 4 a aa b bb
[root@sh02-hap-bss-prod-consul03 test]# rm -f [!0-9]*
[root@sh02-hap-bss-prod-consul03 test]# ls
1 2 3 4
echo
[root@host1 src]# echo abc ddd
abc ddd
[root@host1 src]# echo -n abc ddd
abc ddd[root@host1 src]#
echo -e
echo -e 處理特殊字符
若字符串中出現(xiàn)以下字符,則特別加以處理,而不會將它當(dāng)成一般文字輸出:
\a 發(fā)出警告聲;
\b 刪除前一個字符;
\c 最后不加上換行符號;
\f 換行但光標(biāo)仍舊停留在原來的位置;
\n 換行且光標(biāo)移至行首;
\r 光標(biāo)移至行首,但不換行;
\t 插入tab;
\v 與\f相同;
\\ 插入\字符;
\nnn 插入nnn(八進(jìn)制)所代表的ASCII字符;
下面舉例說明一下:
$echo -e "a\bdddd" //前面的a會被擦除
dddd
$echo -e "a\adddd" //輸出同時會發(fā)出報警聲音
adddd
$echo -e "a\ndddd" //自動換行
a
dddd
變量
字符串長度: ${#var}
[root@host1 src]# echo ${NODE_HOME}
/usr/local/node
[root@host1 src]# echo ${#NODE_HOME}
15
#長度有15個字符
使用shell進(jìn)行數(shù)學(xué)計算
當(dāng)使用let時,變量名之前不需要添加$
[root@host1 src]# nod1=3
[root@host1 src]# nod2=5
[root@host1 src]# abc=$[nod1+nod2]
[root@host1 src]# echo $abc
8
[root@host1 src]# let def=nod1+nod2
[root@host1 src]# echo $def
8
bc
[root@host3 2056]# echo "4*0.56" |bc
2.24
[root@host3 2056]# no=54
[root@host3 2056]# res=`echo "$no*1.5"|bc`
[root@host3 2056]# echo $res
81.0
[root@host3 2056]#
其他參數(shù)可以置于要執(zhí)行的具體操作之前,同時以分號作為定界符,通過stdin傳遞給bc
例如設(shè)置小數(shù)精度
[root@host3 2056]# echo "scale=2;3/8" | bc
.37
文件描述符
當(dāng)一個命令發(fā)生錯誤并退出時,她會返回一個非0的退出狀態(tài),執(zhí)行成功后會返回數(shù)字0,。退出狀態(tài)可以沖$?中獲得, echo $?
正確輸出到out.txt,錯誤輸出到桌面
ls > out.txt
錯誤輸出到out.txt,正確輸出到桌面
ls 2> out.txt
所有輸出重定向到out.txt
ls &> out.txt
可以聯(lián)合起來
find /etc -name passwd > find.txt 2> find.err
把錯誤結(jié)果丟棄,只輸出正確結(jié)果在屏幕
find /etc -name passwd 2> /dev/null
把所有結(jié)果丟棄
find /etc -name passwd &> /dev/null
由于錯誤的輸出是不能經(jīng)過管道的,所以如果必要,必須把錯誤輸出當(dāng)正確輸出
即:find /etc -name passwd 2>&1 |less
例如find /etc -name passwd |wc -l
實際上這個統(tǒng)計的只有正確的行數(shù),錯誤的輸出沒有統(tǒng)計
find /etc -name passwd 2>&1 |wc -l
這個則把錯誤的也當(dāng)正確的統(tǒng)計出來了
/sbin/service vsftpd stop > /dev/null 2>&1
意思是停止這個服務(wù),正確的輸出丟棄,錯誤輸出當(dāng)正確輸出輸出到終端
數(shù)組和關(guān)聯(lián)數(shù)組
定義數(shù)組的方式有多種,我們常用在單行中只用一列值來定義數(shù)組:
[root@host3 ~]# array_var=(1 2 3 4 5 6 6 6)
[root@host3 ~]# echo ${array_var[*]} #打印數(shù)組中所有值,方式1
1 2 3 4 5 6 6 6
[root@host3 ~]# echo ${array_var[@]} #打印數(shù)組中所有值,方式2
1 2 3 4 5 6 6 6
[root@host3 ~]# echo ${#array_var[*]} #打印數(shù)組長度
8
關(guān)聯(lián)數(shù)組類似于字典,可以自定義key值,可以列出數(shù)組索引key
獲取終端信息
tput sc #存儲光標(biāo)位置
tput rc #恢復(fù)光標(biāo)
tput ed #清除光標(biāo)到行尾的所有內(nèi)容
腳本中生成延時
倒計時:
#!/bin/bash
echo -n Count:
tput sc
count=11;
while true;
do+
if [ $count -gt 0 ];
then
let count--;
sleep 1;
tput rc
tput ed
echo -n $count;
else exit 0;
fi
done
#此處的栗子中,變量count初始值為11,每次循環(huán)便減少1,。tput sc存儲光標(biāo)位置。在每次循環(huán)中,通過恢復(fù)之前存儲的光標(biāo)位置,在終端中打印出新的count值。恢復(fù)光標(biāo)位置的命令是tput rc。 tput ed清除從當(dāng)前光標(biāo)位置到行尾之間的所有內(nèi)容,使得舊的count值可以被清除并寫入新值。
函數(shù)和參數(shù)
定義函數(shù)
function fname()
{
statements;
}
或者:
fname()
{
statements;
}
調(diào)用,只需要使用函數(shù)名字就能調(diào)用
fname; #執(zhí)行函數(shù)
參數(shù)可以傳遞給函數(shù),并有腳本進(jìn)行訪問
fname arg1 arg2;
各種訪問函數(shù)參數(shù)的方法
fname()
{
echo $1,$2; #訪問參數(shù)1和參數(shù)2
echo "$@"; #以列表的形式一次打印所有參數(shù)
echo "$*"; #類似于$@,但是參數(shù)被作為單個實體
echo "$#"; #$#表示這個腳本或者函數(shù)后面參數(shù)的個數(shù)
return 0; #返回值
}
#$@比$*用的多,因為$*把所有參數(shù)當(dāng)做單個字符串,因此很少使用
函數(shù)遞歸
在bash中函數(shù)同樣支持遞歸(可以調(diào)用自身的函數(shù)),例如
F() { echo $1; F hello; sleep 1; }
fork炸彈
:(){ :|:& };:
#這個遞歸函數(shù)就能不斷的調(diào)用自身,生成新的進(jìn)程,最終造成拒絕服務(wù)***,函數(shù)調(diào)用前的&將紫禁城放入后臺。這段危險的代碼會分支處大量的進(jìn)程,因而成為fork炸彈
[root@host3 ~]# :(){ :|:& };:
[1] 2526
[root@host3 ~]#
[1]+ 完成
死機(jī)了
這樣看起來不是很好理解,我們可以更改下格式:
:()
{
:|:&
};
:
更好理解一點的話就是這樣:
bomb()
{
bomb|bomb&
};
bomb
因為shell中函數(shù)可以省略function關(guān)鍵字,所以上面的十三個字符是功能是定義一個函數(shù)與調(diào)用這個函數(shù),函數(shù)的名稱為:,主要的核心代碼是:|:&,可以看出這是一個函數(shù)本身的遞歸調(diào)用,通過&實現(xiàn)在后臺開啟新進(jìn)程運行,通過管道實現(xiàn)進(jìn)程呈幾何形式增長,最后再通過:來調(diào)用函數(shù)引爆炸彈.因此,幾秒鐘系統(tǒng)就會因為處理不過來太多的進(jìn)程而死機(jī),解決的唯一辦法就是重啟
預(yù)防方式
當(dāng)然,F(xiàn)ork炸彈沒有那么可怕,用其它語言也可以分分鐘寫出來一個,例如,python版:
import os
while True:
os.fork()
Fork炸彈的本質(zhì)無非就是靠創(chuàng)建進(jìn)程來搶占系統(tǒng)資源,在Linux中,我們可以通過ulimit命令來限制用戶的某些行為,運行ulimit -a可以查看我們能做哪些限制:
[root@host3 ~]# ulimit -a
core file size (blocks, -c) 0
data seg size (kbytes, -d) unlimited
scheduling priority (-e) 0
file size (blocks, -f) unlimited
pending signals (-i) 7675
max locked memory (kbytes, -l) 64
max memory size (kbytes, -m) unlimited
open files (-n) 655350
pipe size (512 bytes, -p) 8
POSIX message queues (bytes, -q) 819200
real-time priority (-r) 0
stack size (kbytes, -s) 8192
cpu time (seconds, -t) unlimited
max user processes (-u) 100
virtual memory (kbytes, -v) unlimited
file locks (-x) unlimited
可以看到,-u參數(shù)可以限制用戶創(chuàng)建進(jìn)程數(shù),因此,我們可以使用ulimit -u 100來允許用戶最多創(chuàng)建100個進(jìn)程。這樣就可以預(yù)防bomb炸彈。但這樣是不徹底的,關(guān)閉終端后這個命令就失效了。我們可以通過修改/etc/security/limits.conf文件來進(jìn)行更深層次的預(yù)防,在文件里添加如下行
* soft nproc 100
* hard nproc 100
讀取命令返回值(狀態(tài))
$? 給出命令的返回值
返回值被成為退出狀態(tài),他可以用來分析命令是否執(zhí)行成功,如果成功,退出狀態(tài)為0,否則非0
將命令序列的輸出讀入變量
利用子shell生成一個獨立的進(jìn)程
子shell本來就是一個獨立的進(jìn)程,可以使用()操作符來定義一個子shell:
pwd;
(cd /bin; ls);
pwd;
#當(dāng)命令在子shell中運行時,不會對當(dāng)前的shell有任何影響,所有的改變僅限于子shell內(nèi)部。例如當(dāng)cd改變子shell的當(dāng)前目錄時,這種變化不會反映到主shell環(huán)境中
read讀取
read用于從鍵盤或者標(biāo)準(zhǔn)輸入中讀取文本。以交互的形式讀取來自用戶的輸入。
任何變成語言的輸入庫大都是從鍵盤讀取輸入;但只有當(dāng)回車鍵按下的時候,才標(biāo)志著輸入完畢。
read提供了一種不需要回車鍵就能搞定這個任務(wù)的方法
read -p "Enter input:" var
#提示讀取
read -n number_of_chars name
read -n 3 var
echo $var
read -d ":" var
echo $var
#以冒號作為輸入行的結(jié)束
運行命令直到執(zhí)行成功
按照以下方式定義函數(shù):
repeat() { while true;do $@ && return; done }
#我們創(chuàng)建了repeat函數(shù),她包含一個無限循環(huán),該循環(huán)執(zhí)行以參數(shù)形式(通過$@訪問)傳入函數(shù)的命令。如果命令執(zhí)行成功,則返回,進(jìn)而退出循環(huán)
一種更快的做法:
在大多數(shù)現(xiàn)代系統(tǒng)中,true是作為一個二進(jìn)制文件來實現(xiàn)的。這就意味著沒執(zhí)行一次while循環(huán),shell就不得不生成一個進(jìn)程。如果不想這樣,可以使用shell內(nèi)檢的":"命令,她總是返回為0的退出碼:
repeat() { while :; do $@ && return; done }
#盡管可讀性不高,但是肯定比上一種方法快
增加延時
屁如,你要從internet上下一個暫時不可用的文件,不過這個文件需要等一段時間就能下載。方法如下:
repeat wget -c http://abc.test.com/software.tar.gz
#如果采用這種形式,需要向服務(wù)器發(fā)送很多數(shù)據(jù),可能對服務(wù)器產(chǎn)生影響,我們可以修改函數(shù),加入一段短暫的延時
repeat() { while :; do $@ && return; sleep30; done }
#這使得命令每30秒運行一次
字段分隔符和迭代器
內(nèi)部字段分隔符IFS是shell腳本中的一個重要概念。他是存儲界定符的環(huán)境變量,是當(dāng)前shell環(huán)境中使用的默認(rèn)存在的認(rèn)定界字符串
IFS的默認(rèn)值為空白字符(換行符,制表符,或者空格)如在shell中默認(rèn)以空格符作為IFS
[root@host3 ~]# data="abc eee ddd fff"
[root@host3 ~]# for item in $data; do echo ITEM: $item; done
ITEM: abc
ITEM: eee
ITEM: ddd
ITEM: fff
執(zhí)行:
list1="1 2 3 3 4 4"
for line in $list1
do
echo $line;
done
輸出:
1
2
3
3
4
4
執(zhí)行:
for line in 1 2 3 3 4 4 #如果將in后面的用引號引起來,就會當(dāng)成一個字符串
do
echo $line;
done
同樣輸出:
1
2
3
3
4
4
接下來我們可以將IFS改成逗號:
#沒有修改IFS,此時我們默認(rèn)是空格符,故將data作為單個字符串打印出來
[root@host3 ~]# data="eee,eee,111,222"
[root@host3 ~]# for item in $data1; do echo ITEM: $item; done
ITEM: eee,eee,111,222
[root@host3 ~]# oldIFS=$IFS #此步驟是為了先備份目前的IFS為oldIFS,后面會恢復(fù)
[root@host3 ~]# IFS=, #備份后修改IFS為逗號,再次輸出則發(fā)現(xiàn)逗號已經(jīng)成為分隔符
[root@host3 ~]# for item in $data1; do echo ITEM: $item; done
ITEM: eee
ITEM: eee
ITEM: 111
ITEM: 222
[root@host3 ~]# IFS=$oldIFS #還原IFS為原來的
[root@host3 ~]# for item in $data1; do echo ITEM: $item; done
ITEM: eee,eee,111,222
故我們再修改了IFS使用之后記得恢復(fù)到原樣
for循環(huán)
for var in list;
do
commands;
done
list可以是一個字符串,也可以是一個序列
{1..50}生成一個1-50的數(shù)字列表
{a..z}或{A..Z}或{a..h}生成字母表
for也可以采用c語言中的for循環(huán)模式
for (i=0;i<10;i++)
{
commands; #使用變量$i
}
while循環(huán)
while condition
do
commands;
done
until循環(huán)
她會一直循環(huán)直到給出的條件為真
x=0;
until [ $x -eq 9 ];
do
let x++; echo $x;
done
比較與測試
程序中的流程控制是比較語句和測試語句處理的。我們可以用if if else以及邏輯運算符進(jìn)行測試,用比較運算符來比較數(shù)據(jù)。除此之外,還有一個test命令也用于測試
if condition;
then
commands;
fi
if condition;
then
commands;
else if condition; then
commands;
else
commands;
fi
if和else語句可以進(jìn)行嵌套,那樣會變得很長,可以用邏輯運算符將他簡潔點
[ condition ] && action; #如果前者為真,則執(zhí)行action;
[ condition ] || action; #如果前者為假,則執(zhí)行action;
算術(shù)比較
條件通常被放置在封閉的中括號內(nèi),一定注意在 [或]與操作數(shù)之間有一個空格,如果忘記了這個空格,就會報錯
算術(shù)判斷:
[ $var -eq 0 ] #當(dāng)$var等于0時,返回真
[ $var -ne 0 ] #當(dāng)$var為非0時,返回真
其他:
-gt: 大于
-lt:小于
-ge:大于或等于
-le:小于或等于
多條件測試:
[ $var1 -ne 0 -a $var2 -gt 2 ] #邏輯與 -a
[ $var1 -ne 0 -o $var2 -gt 2 ] #邏輯或 -o
文件系統(tǒng)相關(guān)測試
我們可以使用不同的條件標(biāo)志測試不同的文件系統(tǒng)相關(guān)的屬性:
[ -f file ] 給定的變量包含正常的文件路徑或文件名,則為真
[ -x file ] 可執(zhí)行,為真
[ -d file ] 是目錄,為真
[ -e file ] 文件存在,則為真
[ -w file ] 可寫,則為真
[ -r file ] 可讀,則為真
[ -L file ] 包含的是一個符號鏈接,則為真
使用方法如下:
fpath="/etc/passwd"
if [ -e $fpath ];then
echo File exists;
else
echo Dose not exists;
fi
字符串比較
使用字符串比較時,最好用雙中括號,因為有時候采用單個中括號會產(chǎn)生錯誤,所以最好避開
可以使用下面的方法測試兩個字符串,看看是否相同
[[ $str1 = $str2 ]]
或者:
[[ $str1 == $str2 ]]
反之:
[[ $str1 != $str2 ]]
[[ -z $str1 ]] 為空字符串,則返回真
[[ -n $str1 ]] 為非空字符串,則返回真
一般寫法:
逆序打印命令tac,這個和cat相反
cat file1 file2 file3 ...
這個命令將命令行參數(shù)的文件內(nèi)容拼接在一起
類似的,我們可以用cat將來自輸入文件的內(nèi)容與標(biāo)準(zhǔn)輸入拼接到一起,將stdin和另外一個文件中的數(shù)據(jù)結(jié)合起來,方法如下:
echo "111111" |cat - /etc/passwd
上面的代碼中,-被作為stdin文本的文件名
將制表符顯示為^I
例如在用python編寫程序時,代碼縮進(jìn)用制表符和空格是不同的,如果在空格的地方使用了制表符,就會發(fā)生縮進(jìn)錯誤。僅僅在文本編輯器中很難發(fā)現(xiàn)這種錯誤
此時,我們可以用-T選項顯示出制表符,標(biāo)記成 ^I
[root@host3 ~]# cat bbb.sh
for line in "1 2 3 3 4 4"
do
echo $line;
done
[root@host3 ~]# cat -T bbb.sh
for line in "1 2 3 3 4 4"
do
^Iecho $line;
done
行號 cat -n
#-n會為空白行也加上行號,如果需要跳過空白行,那么可以使用選項-b
[root@host3 ~]# cat bbb.sh
for line in "1 2 3 3 4 4"
do
echo $line;
done
[root@host3 ~]# cat -n bbb.sh
1 for line in "1 2 3 3 4 4"
2
3 do
4 echo $line;
5 done
[root@host3 ~]# cat -b bbb.sh
1 for line in "1 2 3 3 4 4"
2 do
3 echo $line;
4 done
find 命令的工作方式是,沿著文件層次結(jié)構(gòu)向下遍歷,匹配符合條件的文件,執(zhí)行相應(yīng)操作
find /etc #列出目錄下所有的文件和文件夾,包括隱藏文件
find -iname 忽略大小寫
#匹配一個或者多個文件時候,可以用OR條件操作,如查找/etc下所有.txt和.conf文件
find /etc \( -name "*.txt" -o -name "*.conf" \)
find /etc \( -name "*.txt" -o -name "*.conf" \) -print
#\(以及\)用于將-name "*.txt" -o -name "*.conf"視為一個整體
#-name用來匹配文件,-path則用來匹配文件路徑,可用通配符
find / -path "*/etc/*" -print
#打印只要路徑中包含/etc/的及打印
#-regex參數(shù),正則則更加強(qiáng)大。例如email地址可以常用name@host.root這種形式。所以將其一般轉(zhuǎn)化為:
#[a-z0-9]+@[a-z0-9]+.[a-z0-9]+
#符號+指明在它之前的字符類中字符可以出現(xiàn)一次或者多次。
find /etc -regex ".*\(\.py|\.sh\)$"
#查找以.py或.sh結(jié)尾的所有文件
#同樣 -iregex也可以忽略大小寫,同-iname一樣
#-regex同樣屬于測試項。使用-regex時有一點要注意:-regex不是匹配文件名,而是匹配完整的文件名(包括路徑)。例如,當(dāng)前目錄下有一個文件"abar9",如果你用"ab.*9"來匹配,將查找不到任何結(jié)果,正確的方法是使用".*ab.*9"或者".*/ab.*9"來匹配。
find . -regex ".*/[0-9]*/.c" -print
否定參數(shù)
find /etc ! -name "*.conf" -print
基于目錄深度的搜索
我們可以采用深度選項 -maxdepth和-mindepth來限制find命令遍歷的目錄深度
[root@host3 ~]# find /etc -maxdepth 1 -name "*.conf" -print
/etc/resolv.conf
/etc/dracut.conf
/etc/host.conf
[root@host3 ~]# find /etc -maxdepth 2 -name "*.conf" -print
/etc/resolv.conf
/etc/depmod.d/dist.conf
[root@host3 ~]# find /etc -mindepth 4 -name "*.conf" -print
/etc/openldap/slapd.d/openldap/ldap.conf
/etc/openldap/slapd.d/openldap/schema/schema_convert.conf
/etc/openldap/slapd.d/openldap/slapd.conf
基于時間的進(jìn)行搜索
-atime:最近一次訪問時間
-mtime:最近一次修改時間
-ctime:文件元數(shù)據(jù)(例如權(quán)限或者所有權(quán))最后一次改變時間
上面都是以天為單位
也有以分鐘為單位:
-amin
-mmin
-cmin
-newer,參考文件,比較時間戳。比參考文件更新的文件
[root@host3 ~]# find /etc -type f -newer /etc/passwd -print
/etc/resolv.conf
/etc/shadow
/etc/ld.so.cache
/etc/cni/net.d/calico-kubeconfig
基于文件大小的搜索
find /etc -type f -size +2k #大于2k
find /etc -type f -size -2k #小于2k
find /etc -type f -size 2k #等于2k
刪除匹配的文件
find ./ -type f -name "*.txt" -delete
基于文件和所有權(quán)
find /etc -type f -perm 644
find /etc -type f -name "*.conf" ! -perm 644
基于用戶進(jìn)行搜索
find /etc -type f -user USER
執(zhí)行命令或者動作
find /etc -type f -user root -exec chown MySQL {} \;
#將所有所有人是root的文件所有人改成mysql
# {}是一個和-exec選項搭配使用的特殊字符串。對于每一個匹配文件, {}會被替換成相應(yīng)的文件名。
另外一個例子就是將給定目錄中的所有文件內(nèi)容拼接起來寫入單個文件,我們可以用find找到所有.conf文件,然后結(jié)合-exec使用cat命令:
find /etc/ -type f -name "*.conf" -exec cat {} \;>all.txt
#即將所有.conf文件的內(nèi)容全部追加寫入all.txt文件里
#沒有用>>追加的原因是因為find命令全部輸出就只有一個數(shù)據(jù)流(stdin),而只有當(dāng)多個數(shù)據(jù)流被追加到單個文件時才有必要使用
#下面命令將10天前的.txt文件復(fù)制到OLD目錄:
find /etc -type f -mtime +10 -name "*.txt" -exec cp {} OLD \;
讓find跳過一些目錄
有時候為了提高性能,需要跳過一些目錄,例如git,每個子目錄中都會包含一個.git目錄,要跳過這些目。
find /etc \( -name ".git" -prune \) -o \( -type f -print \)
#\( -name "/etc/rabbitmq" -prune \)的作用是用于排除,而\( -type f -print \)指明需要執(zhí)行的動作。
xargs命令把從stdin接收到的數(shù)據(jù)重新格式化,再將其作為參數(shù)提供給其他命令
xargs作為一種替代,其作用類似于find命令中的-exec
[root@host3 ~]# cat 123.txt
1 2 3 4 5
6 7 8 9
10 11 12 13 14
[root@host3 ~]# cat 123.txt |xargs
1 2 3 4 5 6 7 8 9 10 11 12 13 14
[root@host3 ~]# cat 123.txt
1 2 3 4 5
6 7 8 9
10 11 12 13 14
[root@host3 ~]# cat 123.txt |xargs -n 3
1 2 3
4 5 6
7 8 9
10 11 12
13 14
[root@host3 ~]# echo 1 3 4 5 6 7 8 |xargs -n 3
1 3 4
5 6 7
8
[root@host3 ~]# echo "abcTdslfjTdshfsT1111Tfd222" |xargs -d T
abc dslfj dshfs 1111 fd222
#以字母T作為分隔符
#我們可以自定義分解符的同時每行定義輸出多少個參數(shù)
[root@host3 ~]# echo "abcTdslfjTdshfsT1111Tfd222" |xargs -d T -n 2
abc dslfj
dshfs 1111
fd222
#每行輸出一個參數(shù)
[root@host3 ~]# echo "abcTdslfjTdshfsT1111Tfd222" |xargs -d T -n 1
abc
dslfj
dshfs
1111
fd222
cmd0 | (cmd1;cmd2;cmd3) | cmd4
中間是子shell,里面如果有cmd,只在子shell內(nèi)生效
-print 在每一個輸出后會添加一個回車換行符,而-print0則不會。
[root@AaronWong shell_test]# find /home/AaronWong/ABC/ -type f -print
/home/AaronWong/ABC/libcvaux.so
/home/AaronWong/ABC/libgomp.so.1
/home/AaronWong/ABC/libcvaux.so.4
/home/AaronWong/ABC/libcv.so
/home/AaronWong/ABC/libhighgui.so.4
/home/AaronWong/ABC/libcxcore.so
/home/AaronWong/ABC/libhighgui.so
/home/AaronWong/ABC/libcxcore.so.4
/home/AaronWong/ABC/libcv.so.4
/home/AaronWong/ABC/libgomp.so
/home/AaronWong/ABC/libz.so
/home/AaronWong/ABC/libz.so.1
[root@AaronWong shell_test]# find /home/AaronWong/ABC/ -type f -print0
/home/AaronWong/ABC/libcvaux.so/home/AaronWong/ABC/libgomp.so.1/home/AaronWong/ABC/libcvaux.so.4/home/AaronWong/ABC/libcv.so/home/AaronWong/ABC/libhighgui.so.4/home/AaronWong/ABC/libcxcore.so/home/AaronWong/ABC/libhighgui.so/home/AaronWong/ABC/libcxcore.so.4/home/AaronWong/ABC/libcv.so.4/home/AaronWong/ABC/libgomp.so/home/AaronWong/ABC/libz.so/home/AaronWong/ABC/libz.so.1
tr只能通過stdin標(biāo)準(zhǔn)輸入,而無法通過命令行參數(shù)來接收輸入。他的調(diào)用格式為:
tr [option] set1 set2
制表符轉(zhuǎn)換成空格: tr '\t' ' ' < file.txt
[root@host3 ~]# cat -T 123.txt
1 2 3 4 5
6 7 8 9
^I10 11 12 13 14
[root@host3 ~]# tr '\t' ' ' < 123.txt
1 2 3 4 5
6 7 8 9
10 11 12 13 14
tr有一個選項-d,可以通過指定需要被刪除的字符集合,將出現(xiàn)在stdin中的特定字符清除掉:
cat file.txt |tr -d '[set1]'
#只使用set1不使用set2
#替換數(shù)字
[root@host3 ~]# echo "Hello 123 world 456" |tr -d '0-9'
Hello world
#替換字母
[root@host3 ~]# echo "Hello 123 world 456" |tr -d 'A-Za-z'
123 456
#替換H
[root@host3 ~]# echo "Hello 123 world 456" |tr -d 'H'
ello 123 world 456
sort能幫助我們對文本文件和stdin進(jìn)行排序操作。他通常配合其他命令來生成所需要的輸出。uniq是一個經(jīng)常與sort一同使用的命令。他的作用是從文本或stdin中提取唯一的行。
#我們可以按照下面的方式輕松的對一組文件(例如file1.txt file2.txt)進(jìn)行排序:
[root@host3 ~]# sort /etc/passwd /etc/group
adm:x:3:4:adm:/var/adm:/sbin/nologin
adm:x:4:
apache:x:48:
apache:x:48:48:Apache:/usr/share/httpd:/sbin/nologin
audio:x:63:
bin:x:1:
bin:x:1:1:bin:/bin:/sbin/nologin
caddy:x:996:
caddy:x:997:996:Caddy web server:/var/lib/caddy:/sbin/nologin
...
#也可合并排序后重定向到新文件
sort /etc/passwd /etc/group > abc.txt
#按照數(shù)字排序
sort -n
#逆序排序
sort -r
#按照月份排序
sort -M month.txt
#合并兩個已經(jīng)排序了的文件
sort -m sorted1 sorted2
#找出已排序文件中不重復(fù)的行
sort file1.txt file2.txt |uniq
檢查文件是否已經(jīng)排序過:
要檢查文件是否排過序,可以采用以下方法,如果已經(jīng)排序,sort會返回0的退出碼($?),否則返回非0
#!/bin/bash
sort -C filename;
if [ $? -eq 0 ]; then
echo Sorted;
else
echo Unsorted;
fi
sort命令包含大量選項。如果使用uniq,那sort更加必不可少,因為需求輸入數(shù)據(jù)必須經(jīng)過排序
sort完成一些較復(fù)雜的任務(wù)
#-k指定了按照哪一列進(jìn)行排序,-r逆序,-n按照數(shù)字
sort -nrk 1 data.txt
sort -k 2 data.txt
uniq
uniq只能作用于排過序的數(shù)據(jù)輸入
[root@host3 ~]# cat data.txt
1010hellothis
3333
2189ababbba
333
7464dfddfdfd
333
#去重
[root@host3 ~]# sort data.txt |uniq
1010hellothis
2189ababbba
333
3333
7464dfddfdfd
#去重并統(tǒng)計
[root@host3 ~]# sort data.txt |uniq -c
1 1010hellothis
1 2189ababbba
2 333
1 3333
1 7464dfddfdfd
#只顯示文本中沒有重復(fù)的行
[root@host3 ~]# sort data.txt |uniq -u
1010hellothis
2189ababbba
3333
7464dfddfdfd
#只顯示文本中重復(fù)了的行
[root@host3 ~]# sort data.txt |uniq -d
333
編寫shell腳本時,我們經(jīng)常需要存儲臨時數(shù)據(jù)。最適合存儲臨時數(shù)據(jù)的位置是/tmp(該目錄中的內(nèi)容在系統(tǒng)重啟后會被清空)。有兩種方法可以為臨時數(shù)據(jù)生成標(biāo)準(zhǔn)的文件名
[root@host3 ~]# file1=`mktemp`
[root@host3 ~]# echo $file1
/tmp/tmp.P9var0Jjdw
[root@host3 ~]# cd /tmp/
[root@host3 tmp]# ls
add_user_ldapsync.ldif create_module_config.ldif.bak globalconfig.ldif overlay.ldif
create_module_config.ldif databaseconfig_nosyncrepl.ldif initial_structure.ldif tmp.P9var0Jjdw
#上面的代碼創(chuàng)建了一個臨時文件,并且打印出文件名
[root@host3 tmp]# dir1=`mktemp -d`
[root@host3 tmp]# echo $dir1
/tmp/tmp.UqEfHa389N
[root@host3 tmp]# ll
總用量 28
-r--------. 1 root root 130 2月 12 2019 add_user_ldapsync.ldif
-r--------. 1 root root 329 2月 14 2019 create_module_config.ldif
-r--------. 1 root root 329 2月 12 2019 create_module_config.ldif.bak
-r--------. 1 root root 2458 2月 14 2019 databaseconfig_nosyncrepl.ldif
-r--------. 1 root root 239 2月 12 2019 globalconfig.ldif
-r--------. 1 root root 795 2月 12 2019 initial_structure.ldif
-r--------. 1 root root 143 2月 12 2019 overlay.ldif
-rw------- 1 root root 0 9月 27 13:06 tmp.P9var0Jjdw
drwx------ 2 root root 6 9月 27 13:09 tmp.UqEfHa389N
#以上代碼創(chuàng)建了一個臨時目錄,并打印目錄名
[root@host3 tmp]# mktemp test1.XXX
test1.mBX
[root@host3 tmp]# mktemp test1.XXX
test1.wj1
[root@host3 tmp]# ls
總用量 28
-r--------. 1 root root 130 2月 12 2019 add_user_ldapsync.ldif
-r--------. 1 root root 329 2月 14 2019 create_module_config.ldif
-r--------. 1 root root 329 2月 12 2019 create_module_config.ldif.bak
-r--------. 1 root root 2458 2月 14 2019 databaseconfig_nosyncrepl.ldif
-r--------. 1 root root 239 2月 12 2019 globalconfig.ldif
-r--------. 1 root root 795 2月 12 2019 initial_structure.ldif
-r--------. 1 root root 143 2月 12 2019 overlay.ldif
-rw------- 1 root root 0 9月 27 13:12 test1.mBX
-rw------- 1 root root 0 9月 27 13:12 test1.wj1
-rw------- 1 root root 0 9月 27 13:06 tmp.P9var0Jjdw
drwx------ 2 root root 6 9月 27 13:09 tmp.UqEfHa389N
#以上是根據(jù)模板名創(chuàng)建臨時文件,XXX為大寫,X會被隨機(jī)的字符字母或者數(shù)字替換,注意mktemp正常工作的前提是保證模板中至少有3個X
假設(shè)一個data.txt的測試文件,大小為100kb,你可以將他分割為多個大小為10kb的文件
[root@host3 src]# ls
nginx-1.14.2 nginx-1.14.2.tar.gz
[root@host3 src]# du -sh nginx-1.14.2.tar.gz
992K nginx-1.14.2.tar.gz
[root@host3 src]# split -b 100k nginx-1.14.2.tar.gz
[root@host3 src]# ll
×üó?á? 1984
drwxr-xr-x 9 postgres mysql 186 8?? 15 19:50 nginx-1.14.2
-rw-r--r-- 1 root root 1015384 8?? 16 10:44 nginx-1.14.2.tar.gz
-rw-r--r-- 1 root root 102400 9?? 29 12:36 xaa
-rw-r--r-- 1 root root 102400 9?? 29 12:36 xab
-rw-r--r-- 1 root root 102400 9?? 29 12:36 xac
-rw-r--r-- 1 root root 102400 9?? 29 12:36 xad
-rw-r--r-- 1 root root 102400 9?? 29 12:36 xae
-rw-r--r-- 1 root root 102400 9?? 29 12:36 xaf
-rw-r--r-- 1 root root 102400 9?? 29 12:36 xag
-rw-r--r-- 1 root root 102400 9?? 29 12:36 xah
-rw-r--r-- 1 root root 102400 9?? 29 12:36 xai
-rw-r--r-- 1 root root 93784 9?? 29 12:36 xaj
[root@host3 src]# ls
nginx-1.14.2 nginx-1.14.2.tar.gz xaa xab xac xad xae xaf xag xah xai xaj
[root@host3 src]# du -sh *
32M nginx-1.14.2
992K nginx-1.14.2.tar.gz
100K xaa
100K xab
100K xac
100K xad
100K xae
100K xaf
100K xag
100K xah
100K xai
92K xaj
#如上,將992K的nginx tar包分成了100k一個,最后不足100k只有92k
由上面的可以看出來,默認(rèn)是以字母為后綴。如果想以數(shù)字為后綴,可以使用-d參數(shù),-a length指定后綴長度
[root@host3 src]# ls
nginx-1.14.2 nginx-1.14.2.tar.gz
[root@host3 src]# split -b 100k nginx-1.14.2.tar.gz -d -a 5
[root@host3 src]# ls
nginx-1.14.2 nginx-1.14.2.tar.gz x00000 x00001 x00002 x00003 x00004 x00005 x00006 x00007 x00008 x00009
[root@host3 src]# du -sh *
32M nginx-1.14.2
992K nginx-1.14.2.tar.gz
100K x00000
100K x00001
100K x00002
100K x00003
100K x00004
100K x00005
100K x00006
100K x00007
100K x00008
92K x00009
#文件名為x后綴為5位數(shù)數(shù)字
指定文件名前綴
之前分割的文件,文件都有一個文件名x,我們也可以通過前綴名來使用自己的文件前綴。split命令最后一個參數(shù)是PREFIX
[root@host3 src]# ls
nginx-1.14.2 nginx-1.14.2.tar.gz
[root@host3 src]# split -b 100k nginx-1.14.2.tar.gz -d -a 4 nginxfuck
[root@host3 src]# ls
nginx-1.14.2 nginxfuck0000 nginxfuck0002 nginxfuck0004 nginxfuck0006 nginxfuck0008
nginx-1.14.2.tar.gz nginxfuck0001 nginxfuck0003 nginxfuck0005 nginxfuck0007 nginxfuck0009
#如上,最后一個參數(shù)指定了前綴
如果不想根據(jù)大小來分割,我們可以根據(jù)行數(shù)來分割-l
[root@host3 test]# ls
data.txt
[root@host3 test]# wc -l data.txt
7474 data.txt
[root@host3 test]# split -l 1000 data.txt -d -a 4 conf
[root@host3 test]# ls
conf0000 conf0001 conf0002 conf0003 conf0004 conf0005 conf0006 conf0007 data.txt
[root@host3 test]# du -sh *
40K conf0000
48K conf0001
48K conf0002
36K conf0003
36K conf0004
36K conf0005
36K conf0006
20K conf0007
288K data.txt
#以上將一個7000行的文件分成1000行一份,文件名以conf開頭,后接4位數(shù)字
csplit能依據(jù)指定的條件和字符串匹配選項對日志文件進(jìn)行分割,是split工具的一個變體
split只能根據(jù)數(shù)據(jù)的大小和行數(shù)進(jìn)行分割,而csplit可以根據(jù)文件自身的特點進(jìn)行分割。是否存在某個單詞或文本內(nèi)容都可以作為分割文件的條件
[root@host3 test]# ls
data.txt
[root@host3 test]# cat data.txt
SERVER-1
[conection] 192.168.0.1 success
[conection] 192.168.0.2 failed
[conection] 192.168.0.3 success
[conection] 192.168.0.4 success
SERVER-2
[conection] 192.168.0.5 success
[conection] 192.168.0.5 failed
[conection] 192.168.0.5 success
[conection] 192.168.0.5 success
SERVER-3
[conection] 192.168.0.6 success
[conection] 192.168.0.7 failed
[conection] 192.168.0.8 success
[conection] 192.168.0.9 success
[root@host3 test]# csplit data.txt /SERVER/ -n 2 -s {*} -f server -b "%02d.log";rm server00.log
rm:是否刪除普通空文件 "server00.log"?y
[root@host3 test]# ls
data.txt server01.log server02.log server03.log
詳細(xì)說明:
因為分割后的第一個文件沒有任何內(nèi)容(匹配的單詞就位于文件的第一行),所以我們刪除server00.log
有一些腳本時依據(jù)文件名進(jìn)行各種處理的,我們可能需要在保留擴(kuò)展名的同時修改文件名,轉(zhuǎn)換文件格式(保留文件名的同時修改擴(kuò)展名)或提取部分文件名。shell所具有的一些內(nèi)建功能可以依據(jù)不同的情況來切分文件名
借助%符號可以輕松將名稱部分從"名稱.擴(kuò)展名"這種格式中提取出來。
[root@host3 ~]# file_jpg="test.jpg"
[root@host3 ~]# name=${file_jpg%.*}
[root@host3 ~]# echo $name
test
#即提取了文件名部分
借助#符號則可以將文件名的擴(kuò)展名部分提取出來。
[root@host3 ~]# file_jpg="test.jpg"
[root@host3 ~]# exten=${file_jpg#*.}
[root@host3 ~]# echo $exten
jpg
#提取擴(kuò)展名,上面提取文件名部分是.* 此處提取擴(kuò)展名為*.
以上語法釋義
${VAR%.*}含義:
%屬于非貪婪(non-greedy)操作,他從右到左找出匹配通配符的最短結(jié)果。還有另一個操作符%%,這個操作符與%相似,但行為模式卻是貪婪的,這意味著她會匹配符合條件的最長的字符串,例如VAR=hack.fun.book.txt
使用%操作符:
[root@host3 ~]# VAR=hack.fun.book.txt
[root@host3 ~]# echo ${VAR%.*}
hack.fun.book
使用%%操作符:
[root@host3 ~]# echo ${VAR%%.*}
hack
同樣,對于#操作符也有##
使用#操作符:
[root@host3 ~]# echo ${VAR#*.}
fun.book.txt
使用##操作符
[root@host3 ~]# echo ${VAR##*.}
txt
綜合運用find,rename,mv我們能做到很多事情
用特定的格式重命名當(dāng)前目錄下的圖像文件,最簡單的方法就是運用以下的腳本
#!/bin/bash
count=1;
for img in `find . -iname '*.png' -o -iname '*.jpg' -type f -maxdepth 1`
do
new=image-$count.${img##*.}
echo "Rename $img to $new"
mv $img $new
let count++
done
執(zhí)行上面腳本
[root@host3 ~]# ll
總用量 24
-rw-r--r-- 1 root root 0 10月 8 14:22 aaaaaa.jpg
-rw-r--r-- 1 root root 190 8月 9 13:51 aaa.sh
-rw-r--r-- 1 root root 2168 9月 24 10:15 abc.txt
-rw-r--r-- 1 root root 3352 9月 20 09:58 all.txt
-rw-------. 1 root root 1228 1月 8 2019 anaconda-ks.cfg
-rw-r--r-- 1 root root 0 10月 8 14:22 bbbb.jpg
-rw-r--r-- 1 root root 48 9月 18 10:27 bbb.sh
-rw-r--r-- 1 root root 0 10月 8 14:22 cccc.png
drwxr-xr-x 2 root root 333 4月 11 19:21 conf
-rw-r--r-- 1 root root 0 10月 8 14:22 dddd.png
-rw-r--r-- 1 root root 190 10月 8 14:22 rename.sh
[root@host3 ~]# sh rename.sh
find: 警告: 您在非選項參數(shù) -iname 后定義了 -maxdepth 選項,但選項不是位置選項 (-maxdepth 影響在它之前或之后的指定的比較測試)。請在其它參數(shù)之前指定選項。
Rename ./aaaaaa.jpg to image-1.jpg
Rename ./bbbb.jpg to image-2.jpg
Rename ./cccc.png to image-3.png
Rename ./dddd.png to image-4.png
[root@host3 ~]# ls
aaa.sh abc.txt all.txt anaconda-ks.cfg bbb.sh conf image-1.jpg image-2.jpg image-3.png image-4.png rename.sh
先寫一個讀取交互式輸入的腳本
#!/bin/bash
#文件名: test.sh
read -p "Enter number:" no
read -p "Enter name:" name
echo $no,$name
按照下面的方法自動向腳本發(fā)送輸入:
[root@host3 ~]# ./test.sh
Enter number:2
Enter name:rong
2,rong
[root@host3 ~]# echo -e "2\nrong\n" |./test.sh
2,rong
# \n代表著回車,我們用echo -e來生成輸入序列,-e表明echo會解釋轉(zhuǎn)義序列。如果輸入內(nèi)容較多,那么可以單獨的輸入文件結(jié)合重定向操作符來提供輸入,如下:
[root@host3 ~]# echo -e "2\nrong\n" > input.data
[root@host3 ~]# cat input.data
2
rong
[root@host3 ~]# ./test.sh < input.data
2,rong
#這個方法是從文件中導(dǎo)入交互式輸入數(shù)據(jù)
如果你是逆向工程師,那可能同緩沖區(qū)溢出打過交道。要實施,我們需要將十六進(jìn)制形式的shellcode(例如"\xeb\x1a\x5e\x31\xc0\x88\x46")進(jìn)行重定向。這些字符沒法直接通過鍵盤輸入,因為鍵盤上并沒有對應(yīng)的按鍵。因此我們應(yīng)該使用:
echo -e "\xeb\x1a\x5e\x31\xc0\x88\x46"
用這條命令將shellcode重定向到有缺陷的可執(zhí)行文件中,為了處理動態(tài)輸入并通過檢查程序運行時的輸入需求內(nèi)容來提供輸入內(nèi)容,我們要使用一個出色的工具expect。
expect命令可以根據(jù)輸入要求提供合適的輸入
用expect實現(xiàn)自動化
在默認(rèn)的linux發(fā)行版中,多數(shù)不包含expect,你得自行安裝 :yum -y install expect
#!/usr/bin/expect
# 文件名expect.sh
spawn ./test.sh
expect "Enter number:"
send "2\n"
expect "Enter name:"
send "rong\n"
expect eof
#執(zhí)行
[root@host3 ~]# ./expect.sh
spawn ./test.sh
Enter number:2
Enter name:rong
2,rong
拿md5sum命令為例。由于涉及運算,該命令屬于cpu密集型命令。如果多個文件需要生成校驗和,我們可以使用下面的腳本來運行。
#!/bin/bash
PIDARRAY=()
for file in `find /etc/ -name "*.conf"`
do
md5sum $file &
PIDARRAY+=("$!")
done
wait ${PIDARRAY[@]}
執(zhí)行:
[root@host3 ~]# sh expect.sh
72688131394bcce818f818e2bae98846 /etc/modprobe.d/tuned.conf
77304062b81bc20cffce814ff6bf8ed5 /etc/modprobe.d/firewalld-sysctls.conf
649f5bf7c0c766969e40b54949a06866 /etc/dracut.conf
d0f5f705846350b43033834f51c9135c /etc/prelink.conf.d/nss-softokn-prelink.conf
0335aabf8106f29f6857d74c98697542 /etc/prelink.conf.d/fipscheck.conf
0b501d6d547fa5bb989b9cb877fee8cb /etc/modprobe.d/dccp-blacklist.conf
d779db0cc6135e09b4d146ca69d39c2b /etc/rsyslog.d/listen.conf
4eaff8c463f8c4b6d68d7a7237ba862c /etc/resolv.conf
321ec6fd36bce09ed68b854270b9136c /etc/prelink.conf.d/grub2.conf
3a6a059e04b951923f6d83b7ed327e0e /etc/depmod.d/dist.conf
7cb6c9cab8ec511882e0e05fceb87e45 /etc/systemd/bootchart.conf
2ad769b57d77224f7a460141e3f94258 /etc/systemd/coredump.conf
f55c94d000b5d62b5f06d38852977dd1 /etc/dbus-1/system.d/org.freedesktop.hostname1.conf
7e2c094c5009f9ec2748dce92f2209bd /etc/dbus-1/system.d/org.freedesktop.import1.conf
5893ab03e7e96aa3759baceb4dd04190 /etc/dbus-1/system.d/org.freedesktop.locale1.conf
f0c4b315298d5d687e04183ca2e36079 /etc/dbus-1/system.d/org.freedesktop.login1.conf
···
#由于是多個md5sum命令同時運行的,如果你使用的是多核處理器,就會更快的活的運行結(jié)果
工作原理:
利用bash的操作符&,它使得shell將命令放置于后臺并繼續(xù)執(zhí)行腳本。這意味著一旦循環(huán)結(jié)束,腳本就會退出,而md5sum命令仍然在后臺運行。為了避免這種情況,我們使用$!來獲取進(jìn)程pid,在bash中$!保存這最近一個后臺進(jìn)程的pid,我們將這些pid放入數(shù)組,然后用wait命令等待這些進(jìn)程結(jié)束。
comm命令可以用于兩個文件之間的比較
需要注意的是,comm必須使用排過序的文件作為輸出
[root@host3 ~]# cat a.txt
apple
orange
gold
silver
steel
iron
[root@host3 ~]# cat b.txt
orange
gold
cookies
carrot
[root@host3 ~]# sort a.txt -o A.txt
[root@host3 ~]# vim A.txt
[root@host3 ~]# sort b.txt -o B.txt
[root@host3 ~]# comm A.txt B.txt
apple
carrot
cookies
gold
iron
orange
silver
steel
#可以看出結(jié)果是3列,第一列輸出只在A.txt中存在的行,第二列輸出只在B.txt中出現(xiàn)的行,第三列包含A.txt和B.txt中都存在的行,各列以制表符(\t)作為界定符
#為了打贏交集,我們需要刪除第一列和第二列,只顯示第三列
[root@host3 ~]# comm A.txt B.txt -1 -2
gold
orange
#只打印不同
[root@host3 ~]# comm A.txt B.txt -3
apple
carrot
cookies
iron
silver
steel
#為了是結(jié)果可讀性強(qiáng),去掉前面的\t制表符
[root@host3 ~]# comm A.txt B.txt -3 |sed 's/^\t//'
apple
carrot
cookies
iron
silver
steel
使文件設(shè)置為不可修改 chattr +i file
[root@host3 ~]# chattr +i passwd
[root@host3 ~]# rm -rf passwd
rm: 無法刪除"passwd": 不允許的操作
[root@host3 ~]# chattr -i passwd
[root@host3 ~]# rm -rf passwd
[root@host3 ~]#
grep可以對多個文件進(jìn)行搜索
[root@host3 ~]# grep root /etc/passwd /etc/group
/etc/passwd:root:x:0:0:root:/root:/bin/bash
/etc/passwd:operator:x:11:0:operator:/root:/sbin/nologin
/etc/passwd:dockerroot:x:996:994:Docker User:/var/lib/docker:/sbin/nologin
/etc/group:root:x:0:
/etc/group:dockerroot:x:994:
grep命令只解釋match_text中的某些特殊字符。如果要使用正則表達(dá)式,需要添加 -E選項。這意味著使用擴(kuò)展正則表達(dá)式?;蛘咭部梢允褂媚J(rèn)允許正則表達(dá)式的egrep命令(經(jīng)過實測不加-E也可以)
#統(tǒng)計文本中包含匹配字符串的行數(shù)
[root@host3 ~]# grep -c root /etc/passwd
3
#打印行號
[root@host3 ~]# grep -n root /etc/passwd
1:root:x:0:0:root:/root:/bin/bash
10:operator:x:11:0:operator:/root:/sbin/nologin
27:dockerroot:x:996:994:Docker User:/var/lib/docker:/sbin/nologin
#搜索多個文件并找出匹配文本位于哪一個文件中-l
[root@host3 ~]# grep root /etc/passwd /etc/group
/etc/passwd:root:x:0:0:root:/root:/bin/bash
/etc/passwd:operator:x:11:0:operator:/root:/sbin/nologin
/etc/passwd:dockerroot:x:996:994:Docker User:/var/lib/docker:/sbin/nologin
/etc/group:root:x:0:
/etc/group:dockerroot:x:994:
[root@host3 ~]# grep root /etc/passwd /etc/group -l
/etc/passwd
/etc/group
#-L則正好相反 ,會列出不匹配的文件名
#忽略大小寫 -i
#多個樣式匹配-e
grep -e "pattern1" -e "pattern2" #匹配包含模式1或者模式2的
[root@host3 ~]# grep -e root -e docker /etc/passwd /etc/group
/etc/passwd:root:x:0:0:root:/root:/bin/bash
/etc/passwd:operator:x:11:0:operator:/root:/sbin/nologin
/etc/passwd:dockerroot:x:996:994:Docker User:/var/lib/docker:/sbin/nologin
/etc/group:root:x:0:
/etc/group:dockerroot:x:994:
/etc/group:docker:x:992:
#還有另外一種方法也可以指定多個樣式,我們可以提供一個樣式條件用于讀取樣式。用-f指定文件,注意pat.file文件中不要包含末尾的空白行等
[root@host3 ~]# cat pat.file
root
docker
[root@host3 ~]# grep -f pat.file /etc/passwd /etc/group
/etc/passwd:root:x:0:0:root:/root:/bin/bash
/etc/passwd:operator:x:11:0:operator:/root:/sbin/nologin
/etc/passwd:dockerroot:x:996:994:Docker User:/var/lib/docker:/sbin/nologin
/etc/group:root:x:0:
/etc/group:dockerroot:x:994:
/etc/group:docker:x:992:
grep搜索中指定或者排除某些文件
grep可以在搜索中指定(include)或者排除(exclude)某些文件。我們通過通配符來指定所include文件或者exclude文件
#目錄中遞歸搜索所有的.c 和.cpp文件
grep root . -r --include *.{c,cpp}
[root@host3 ~]# grep root /etc/ -r -l --include *.conf # 此處的-l指僅列出文件名
/etc/systemd/logind.conf
/etc/dbus-1/system.d/org.freedesktop.hostname1.conf
/etc/dbus-1/system.d/org.freedesktop.import1.conf
/etc/dbus-1/system.d/org.freedesktop.locale1.conf
/etc/dbus-1/system.d/org.freedesktop.login1.conf
/etc/dbus-1/system.d/org.freedesktop.machine1.conf
/etc/dbus-1/system.d/org.freedesktop.systemd1.conf
/etc/dbus-1/system.d/org.freedesktop.timedate1.conf
/etc/dbus-1/system.d/wpa_supplicant.conf
#在搜索中排除所有的README文件
grep root . -r --exclude "README"
#******如果要排除目錄,用--exclude-dir,如果要從文件中讀取排除文件列表,使用--exclude-from FILE*****#
#移除空白行
sed '/^$/d' file # /pattern/d會移除匹配的樣式的行
#直接在文件中進(jìn)行替換,使用指定的數(shù)字替換文件中所有的3位數(shù)的數(shù)字
[root@host3 ~]# cat sed.data
11 abc 111 this 9 file contains 111 11 888 numbers 0000
[root@host3 ~]# sed -i 's/\b[0-9]\{3\}\b/NUMBER/g' sed.data
[root@host3 ~]# cat sed.data
11 abc NUMBER this 9 file contains NUMBER 11 NUMBER numbers 0000
#上面的命令替換了所有的3位數(shù)字。正則表達(dá)式\b[0-9]\{3\}\b用于匹配3位數(shù)字,[0-9]表示數(shù)位取值范圍,也就是從0-9
# {3}表示匹配之前的字符3次。其中的\用于轉(zhuǎn)義
# \b表示單詞邊界
sed -i .bak 's/abc/def/' file
#此時sed不僅執(zhí)行文件內(nèi)容替換,還會創(chuàng)建一個名為file.bak的文件,其中包含著原始文件內(nèi)容的副本
已匹配字符串標(biāo)志&
在sed中,我們可以用&標(biāo)記匹配樣式的字符串,這樣就能夠在替換字符串時使用已匹配的內(nèi)容
[root@host3 ~]# echo this is my sister |sed 's/\w\+/<&>/g' #將所有的單詞替換成帶尖括號的單詞
<this> <is> <my> <sister>
[root@host3 ~]# echo this is my sister |sed 's/\w\+/[&]/g' #將所有的單詞替換成帶方括號的單詞
[this] [is] [my] [sister]
#正則表達(dá)式\w\+匹配每一個單詞,然后我們用[&]替換它,&對應(yīng)于之前匹配到的單詞
引用
sed表達(dá)式通常用單引號來引用。不過也可以用雙引號,我們想在sed表達(dá)式中使用一些變量時,雙引號就派上了用場
[root@host3 ~]# text=hello
[root@host3 ~]# echo hello world |sed "s/$text/HELLO/"
HELLO world
特殊變量:
使用原則:
awk -F: '{print NR}' /etc/passwd #打印每一行的行號
awk -F: '{print NF}' /etc/passwd #打印每一行的列數(shù)
[root@host3 ~]# cat passwd
sshd:x:74:74:Privilege-separated SSH:/var/empty/sshd:/sbin/nologin
postfix:x:89:89::/var/spool/postfix:/sbin/nologin
tcpdump:x:72:72::/:/sbin/nologin
elk:x:1000:1000::/home/elk:/bin/bash
ntp:x:38:38::/etc/ntp:/sbin/nologin
saslauth:x:998:76:Saslauthd user:/run/saslauthd:/sbin/nologin
apache:x:48:48:Apache:/usr/share/httpd:/sbin/nologin
nscd:x:28:28:NSCD Daemon:/:/sbin/nologin
[root@host3 ~]# awk -F: '{print NR}' passwd
1
2
3
4
5
6
7
8
[root@host3 ~]# awk -F: '{print NF}' passwd
7
7
7
7
7
7
7
7
[root@host3 ~]# awk -F: 'END{print NF}' passwd
7
[root@host3 ~]# awk -F: 'END{print NR}' passwd
8
#只使用了end語句,每讀入一行,awk會將NR更新為對應(yīng)的行號,當(dāng)達(dá)到最后一行時NR就是最后一行的行號,于是就是文件的行數(shù)
[root@host3 ~]# awk 'BEGIN{ i=0 } { i++ } END{ print i }' passwd
8
awk拼接:
[root@mgmt-k8smaster01 deployment]# docker images|grep veh
192.168.1.74:5000/veh/zuul 0.0.1-SNAPSHOT.34 41e9c323b825 26 hours ago 172MB
192.168.1.74:5000/veh/vehicleanalysis 0.0.1-SNAPSHOT.38 bca9981ac781 26 hours ago 210MB
192.168.1.74:5000/veh/masterveh 0.0.1-SNAPSHOT.88 265e448020f3 26 hours ago 209MB
192.168.1.74:5000/veh/obugateway 0.0.1-SNAPSHOT.18 a4b3309beccd 8 days ago 182MB
192.168.1.74:5000/veh/frontend 1.0.33 357b20afec08 11 days ago 131MB
192.168.1.74:5000/veh/rtkconsumer 0.0.1-SNAPSHOT.12 4c2e63b5b2f6 2 weeks ago 200MB
192.168.1.74:5000/veh/user 0.0.1-SNAPSHOT.14 015fc6516533 2 weeks ago 186MB
192.168.1.74:5000/veh/rtkgw 0.0.1-SNAPSHOT.12 a17a3eed4d28 2 months ago 173MB
192.168.1.74:5000/veh/websocket 0.0.1-SNAPSHOT.7 a1af778846e6 2 months ago 179MB
192.168.1.74:5000/veh/vehconsumer 0.0.1-SNAPSHOT.20 4a763860a5c5 2 months ago 200MB
192.168.1.74:5000/veh/dfconsumer 0.0.1-SNAPSHOT.41 2e3471d6ca27 2 months ago 200MB
192.168.1.74:5000/veh/auth 0.0.1-SNAPSHOT.4 be5c86dd285b 3 months ago 185MB
[root@mgmt-k8smaster01 deployment]# docker images |grep veh |awk '{a=$1;b=$2;c=(a":"b);print c}'
192.168.1.74:5000/veh/zuul:0.0.1-SNAPSHOT.34
192.168.1.74:5000/veh/vehicleanalysis:0.0.1-SNAPSHOT.38
192.168.1.74:5000/veh/masterveh:0.0.1-SNAPSHOT.88
192.168.1.74:5000/veh/obugateway:0.0.1-SNAPSHOT.18
192.168.1.74:5000/veh/frontend:1.0.33
192.168.1.74:5000/veh/rtkconsumer:0.0.1-SNAPSHOT.12
192.168.1.74:5000/veh/user:0.0.1-SNAPSHOT.14
192.168.1.74:5000/veh/rtkgw:0.0.1-SNAPSHOT.12
192.168.1.74:5000/veh/websocket:0.0.1-SNAPSHOT.7
192.168.1.74:5000/veh/vehconsumer:0.0.1-SNAPSHOT.20
192.168.1.74:5000/veh/dfconsumer:0.0.1-SNAPSHOT.41
192.168.1.74:5000/veh/auth:0.0.1-SNAPSHOT.4
awk工作方式如下:
我們可以將每一行中的第一個字段的值進(jìn)行累加,即列求和
[root@host3 ~]# cat sum.data
1 2 3 4 5 6
2 2 2 2 2 2
3 3 3 3 3 3
5 5 5 6 6 6
[root@host3 ~]# cat sum.data |awk 'BEGIN{ sum=0 } { print $1; sum+=$1 } END { print sum }'
1
2
3
5
11
[root@host3 ~]# awk '{if($2==3)print $0}' sum.data
3 3 3 3 3 3
[root@host3 ~]# awk '{if($2==5)print $0}' sum.data
5 5 5 6 6 6
#每個值加1
[root@host2 ~]# cat passwd
1:2:3:4
5:5:5:5
3:2:3:5
[root@host2 ~]# cat passwd |awk -F: '{for(i=1;i<=NF;i++){$i+=1}}{print $0}'
2 3 4 5
6 6 6 6
4 3 4 6
[root@host2 ~]# cat passwd |awk -F: '{$2=$2+1;print $0}'
1 3 3 4
5 6 5 5
3 3 3 5
[root@host2 ~]# cat passwd |awk -F: '{if($2==2) $2=$2+1;print $0}'
1 3 3 4
5:5:5:5
3 3 3 5
#將所有2替換成jack fuck,需要更規(guī)范的話表達(dá)式也要用圓括號括起來
[root@host2 ~]# cat passwd |awk -F: '{if($2==2) $2="jack fuck";print $0}'
1 jack fuck 3 4
5:5:5:5
3 jack fuck 3 5
[root@host2 ~]# cat passwd |awk -F: '{if($2==2) ($2="jack fuck");print $0}'
1 jack fuck 3 4
5:5:5:5
3 jack fuck 3 5
將外部變量傳遞給awk
#我們借助選項-v可以將外部值傳遞給awk
[root@host3 ~]# VAR1=10000
[root@host3 ~]# echo |awk -v VAR=$VAR1 '{print VAR}'
10000
#輸入來自標(biāo)準(zhǔn)輸出,所以有echo
#還有另外一種靈活的方法可以將多個外部變量傳遞給awk
[root@host3 ~]# VAR1=10000
[root@host3 ~]# VAR2=20000
[root@host3 ~]# echo |awk '{ print v1,v2 }' v1=$VAR1 v2=$VAR2
10000 20000
使用過濾模式對awk處理的行進(jìn)行過濾
[root@host3 ~]# cat sum.data
1 2 3 4 5 6
2 2 2 2 2 2
3 3 3 3 3 3
5 5 5 6 6 6
#行號小于3的行
[root@host3 ~]# awk 'NR<3' sum.data
1 2 3 4 5 6
2 2 2 2 2 2
#行號為1到4之間的行
[root@host3 ~]# awk 'NR==1,NR==3' sum.data
1 2 3 4 5 6
2 2 2 2 2 2
3 3 3 3 3 3
#包含樣式linux的行
awk '/linux/'
#不包含樣式linux的行
awk '!/linux/'
paste
復(fù)制整個網(wǎng)站(爬蟲)
wget有一個選項可以使其像爬蟲一樣以遞歸的方式遍歷網(wǎng)頁上所有的URL鏈接,并逐個下載。這樣一來我們可以得到這個網(wǎng)站的所有頁面
wget --mirror --convert-links www.chinanews.com
[root@host3 tmp]# ls
www.chinanews.com
[root@host3 tmp]# cd www.chinanews.com/
[root@host3 www.chinanews.com]# ls
allspecial auto cj common gangao hb huaren js m piaowu robots.txt sh society taiwan tp
app china cns2012.shtml fileftp gn hr index.html live.shtml part pv scroll-news shipin stock theory
[root@host3 www.chinanews.com]# ll
260
drwxr-xr-x 2 root root 25 10?? 12 14:11 allspecial
drwxr-xr-x 3 root root 23 10?? 12 14:11 app
drwxr-xr-x 3 root root 18 10?? 12 14:11 auto
drwxr-xr-x 2 root root 24 10?? 12 14:11 china
drwxr-xr-x 3 root root 18 10?? 12 14:11 cj
-rw-r--r-- 1 root root 15799 10?? 12 14:11 cns2012.shtml
drwxr-xr-x 3 root root 46 10?? 12 14:11 common
drwxr-xr-x 6 root root 54 10?? 12 14:11 fileftp
drwxr-xr-x 2 root root 24 10?? 12 14:11 gangao
drwxr-xr-x 4 root root 27 10?? 12 14:11 gn
drwxr-xr-x 2 root root 24 10?? 12 14:11 hb
drwxr-xr-x 3 root root 18 10?? 12 14:11 hr
drwxr-xr-x 2 root root 24 10?? 12 14:11 huaren
-rw-r--r-- 1 root root 184362 10?? 12 14:11 index.html
drwxr-xr-x 2 root root 26 10?? 12 14:11 js
#-convert-links指示wget將頁面的鏈接地址轉(zhuǎn)換為本地地址
網(wǎng)頁下下來默認(rèn)是html格式需要瀏覽器去查看,lynx是一個頗有玩頭的基于命令行的瀏覽器,可以利用他獲取純文本形式的網(wǎng)頁
#用lynx 命令-dump選項將網(wǎng)頁的內(nèi)容以ascii編碼的形式存儲到文本文件中
[root@host3 tmp]# yum -y install lynx
[root@host3 tmp]# lynx www.chinanews.com -dump > abc.txt
[root@host3 tmp]# cat abc.txt
...
1. http://www.chinanews.com/kong/2019/10-12/8976714.shtml
2. http://www.chinanews.com/kong/2019/10-12/8976812.shtml
3. http://www.chinanews.com/kong/2019/10-12/8976721.shtml
4. http://www.chinanews.com/kong/2019/10-12/8976690.shtml
5. http://www.chinanews.com/kong/2019/10-12/8976817.shtml
6. http://www.chinanews.com/kong/2019/10-12/8976794.shtml
7. http://www.chinanews.com/kong/2019/10-12/8976853.shtml
8. http://www.chinanews.com/kong/2019/10-12/8976803.shtml
9. http://www.chinanews.com/sh/2019/10-12/8976754.shtml
10. http://www.chinanews.com/tp/chart/index.shtml
11. http://www.chinanews.com/tp/hd2011/2019/10-12/907641.shtml
12. http://www.chinanews.com/tp/hd2011/2019/10-12/907637.shtml
13. http://www.chinanews.com/tp/hd2011/2019/10-12/907651.shtml
14. http://www.chinanews.com/tp/hd2011/2019/10-12/907644.shtml
15. http://www.chinanews.com/tp/hd2011/2019/10-12/907675.shtml
16. http://www.chinanews.com/tp/hd2011/2019/10-12/907683.shtml
17. http://www.chinanews.com/tp/hd2011/2019/10-12/907656.shtml
18. http://www.ecns.cn/video/2019-10-12/detail-ifzpuyxh6816910.shtml
19. http://www.ecns.cn/video/2019-10-11/detail-ifzpuyxh6815962.shtml
20. http://www.ecns.cn/video/2019-10-11/detail-ifzpuyxh6815122.shtml
21. http://www.ecns.cn/video/2019-10-11/detail-ifzpuyxh6815100.shtml
設(shè)置cookie
要指定cookie,使用--cookie "COOKIES"選項
cookies需要以name=value的形式來給出。多個cookie之間使用分號分隔。例如:--cookie "user=slynux;pass=hack"
如果要將cookie另存為一個文件,使用--cookie-jar選項。例如 --cookie-jar cookie_file
設(shè)置用戶代理字符串
如果不指定用戶代理(user agent),一些需要檢驗用戶代理的網(wǎng)頁就無法顯示。你肯定碰到過一些成就的網(wǎng)站只能ie下工作。如果使用其他瀏覽器,這些網(wǎng)站就會提示說她只能IE訪問。這是因為這些網(wǎng)站檢查了用戶代理。你可以用curl來設(shè)置用
網(wǎng)站名稱:linux腳本攻略
文章起源:http://www.rwnh.cn/article44/gjhpee.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供定制網(wǎng)站、、網(wǎng)站營銷、Google、網(wǎng)站導(dǎo)航、手機(jī)網(wǎng)站建設(shè)
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)