1、有兩個文件 a.txt 和 b.txt,把 a.txt 中有的但 b.txt 中沒有的行找出來,並寫入列 c.txt,而後計算 c 的行數;php
註釋:比較兩個文件兩個文件不一樣,能夠用的方法:diff md5sum grep -vf $1 $2(這個命令匹配$1中沒有的,$2中有的)mysql
diff: 比較兩個文件內容的不一樣,沒有不一樣則不輸出內容; diff $1 $2sql
當 $1 內容多,則顯示 < 內容; 當 $2 內容多,則顯示 > 內容;shell
[root@localhost_002 shell100]# diff 1.txt 2.txt #兩個文件內容相同; #比較兩個文件,當1.txt文件內容多了,會顯示在下面: < yhh [root@localhost_002 shell100]# diff 1.txt 2.txt #1.txt文件內容多出yhh; 6d5 < yhh #比較兩個文件,當2.txt文件內容多了,會顯示在下面: < yuanhaohao [root@localhost_002 shell100]# diff 1.txt 2.txt #2.txt文件內容多出yhhaohao; 4a5 > yuanhaohao
md5sum :經過文件會返回 一個值; md5sum 1.txt 當內容相同,則返回的字符串會相等;此處不是適用;數據庫
[root@localhost_002 shell100]# md5sum 1.txt 620dcc1ed298c34ff730989cbd4e7b06 1.txt [root@localhost_002 shell100]# md5sum 2.txt 620dcc1ed298c34ff730989cbd4e7b06 2.txt
grep -f:會檢索兩個文件相同的內容部分; grep -f b.txt a.txt 匹配a.txt 中有的, b.txt中沒有的;bash
[root@localhost_002 shell100]# cat a.txt #a.txt 文件內容; aaa bbb ccc [root@localhost_002 shell100]# cat b.txt #b.txt 文件內容; bbb aaa [root@localhost_002 shell100]# grep -f b.txt a.txt #判斷 a.txt 和 b.txt 共同有的內容; aaa bbb [root@localhost_002 shell100]# grep -vf b.txt a.txt|wc -l #a.txt爲準,判斷a.txt中有,b.txt不存在的內容; 1
註釋:grep -vf b.txt a.txt|wc -l 表示取反,打印出a.txt中存在,b.txt不存在的文件內容;網絡
或者用while 循環遍歷 a.txt ,諑行進行匹配,若是這一行在b.txt中沒有,就直接重定向到c.txt;併發
註釋:在使用 if 判斷時 grep "^$line$" 才能更精確去匹配整行的內容;curl
grep -vf b.txt a.txt 這樣獲得的結果是 a.txt 和 b.txt 中都有的行,而要的結果是 a.txt中有, b.txt 中沒有的的行,原來的基礎上加上 -v 選項便可實現效果;函數
二、把當前用戶下含有全部進程名字中含有"httpd"的進程關閉;
註釋:當前用戶: $USER
[root@localhost_002 shell100]# echo $USER root [root@localhost_002 shell100]# ps -u $USER
註釋:ps -u user 指定用戶名 能夠查看該用戶下的有進程;
使用awk的 $NF 即最後一列的值匹配'httpd'關鍵字的行,再打印第一列 即目標pid,把這些 pid kill掉就能夠了;
ps -u $USER|awk '$NF ~ /httpd/ {print $1}'|xargs kill
[root@localhost_002 shell100]# ps aux |grep httpd root 1697 0.6 1.0 261744 10992 ? Ss 20:41 0:00 /usr/local/apapche2.4/bin/httpd -k restart daemon 1698 0.0 1.0 548572 10180 ? Sl 20:41 0:00 /usr/local/apapche2.4/bin/httpd -k restart daemon 1699 0.0 1.0 548572 10180 ? Sl 20:41 0:00 /usr/local/apapche2.4/bin/httpd -k restart daemon 1700 0.0 1.0 548572 10180 ? Sl 20:41 0:00 /usr/local/apapche2.4/bin/httpd -k restart root 1783 0.0 0.0 112720 972 pts/0 R+ 20:41 0:00 grep --color=auto httpd [root@localhost_002 shell100]# cat 62.sh #!/bin/bash #這個腳本用來批量殺死某個進程的腳本 #日期: 2019-01-20 #做者: yuanhh ps -u $USER|awk '$NF ~ /httpd/ {print $1}'|xargs kill 執行腳本: [root@localhost_002 shell100]# sh -x 62.sh + awk '$NF ~ /httpd/ {print $1}' + xargs kill + ps -u root [root@localhost_002 shell100]# ps aux |grep httpd root 1886 0.0 0.0 112720 972 pts/0 R+ 20:41 0:00 grep --color=auto httpd
註釋:如上httpd 的進程以及被殺死了;
3、用 shell 腳本,以併發進程的形式將 mysql 數據庫全部的表備份到當前目錄,並把全部的表壓縮到一個壓縮包裏面;
假如數據庫爲 mysql ,用戶名是 yuanhh , 密碼爲 nihao123!;
那麼如何實現 併發進程了 : shell 中加上 & 能夠將命令丟到後臺,實現多條命令達到併發的效果;(不過要控制併發數,否則表數量大,服務資源支撐不了);
備份 表的命令: mysqldump -uroot -pnihao123! mysql tbname > taname.sql
解析:首先定義一個併發值變量N;使用mysql -uroot -pnihao123! mysql -e "show tables"|sed '1'd > table.txt 而後把mysql 下的表過濾出來(須要用sed 把第一行刪除)並寫入到臨時文件;
而後wc -l 計算一下table 有多少行;用awk '{print $1}'過濾出行數;
下面那如何一次性去備份全部表,由於併發值設定的 5 ,每次只備份5個表,把表均分紅5個表,保存到一個臨時文件,而後用for 循環一個一個來備份;
那麼如何把表均分紅每一個五份??? 經過wc 列出的行數$n除以 $N併發值 5 則獲得臨時文件的行數,而後經過split -l 5 來平均分割(xaa xab xac xad xae);
還要考慮到一個問題,若是表的總數小於 5 了,就不必再分一個組了;可使用四捨五入的函數來作;
能夠寫一個四捨五入的函數來定義;
div(){ n=`echo "scale=1;$1/$2"|bc` n1=`echo "scale=1;$n+0.5"|bc` echo $n1|cut -d . -f1 } #######n1=$[$n/$N] n1=`div $n $N`
備份;寫一個函數(使用for循環來cat $1(xaa xab xac xad xae)那個文件,而後逐行導出到 $t.txt);由於逐行,因此須要時間;
myd(){ for t in `cat $1`;do mysqldump -uroot -pnihao123\! mysql $t > $t.txt
在用一個for 循環依次把這五個文件循環備份; for f in xaa xab xac xad xae;do myd $f &;done #這個併發的,由於是5個一塊兒執行;
wait :由於運行以上命令須要時間,因此此處使用 wait 命令(當後臺的全部進程執行完成後,wait纔會釋放);不然則處於等待狀態;
[root@localhost_002 shell100]# sleep 10 & [2] 2250 [root@localhost_002 shell100]# wait [2]- 完成 sleep 10
腳本以下:
[root@localhost_002 shell100]# cat 63.sh #!/bin/bash #這個腳本用來併發備份數據庫 #日期: 2019-01-20 #做者: yuanhh #定義併發值爲5 N=5 mysql -uroot -pnihao123! mysql -e "show tables"|sed '1'd > /tmp/table.txt n=`wc -l /tmp/table.txt|awk '{print $1}'` div(){ n=`echo "scale=1;$1/$2"|bc` n1=`echo "scale=1;$n+0.5"|bc` echo $n1|cut -d . -f1 } #n1=$[$n/$N] n1=`div $n $N` split -l $n1 /tmp/table.txt #備份函數; $1是一個文件名; myd(){ for t in `cat $1` do mysqldump -uroot -pnihao123\! mysql $t > $t.sql 2>/dev/null done } for f in xaa xab xac xad xae do myd $f & done #wait 表示等待後臺進程運行完後,纔會釋放; wait tar czf mydb.tar.gz *.sql rm -fr *.sql 執行腳本: [root@localhost_002 shell100]# sh 63.sh [root@localhost_002 shell100]# ls -ld mydb.tar.gz -rw-r--r-- 1 root root 181145 1月 20 22:34 mydb.tar.gz
註釋:如上,有一條命令執行時會顯示警告細信息; mysql -uroot -pnihao123! mysql -e "show tables"|sed '1'd|head -n3
[root@localhost_002 ~]# mysql -uroot -pnihao123! mysql -e "show tables"|sed '1'd|head -n3 Warning: Using a password on the command line interface can be insecure. columns_priv db event
其實這個 waring 並不會影響腳本的正常運行,可是讓人看着很不爽,不過能夠去掉的了; 使用 mylogin.cnf
mysql_config_editor set --user=root --host=127.0.0.1 --port=3306 --passwd
[root@localhost_002 ~]# mysql_config_editor set --user=root --password Enter password: [root@localhost_002 ~]# ls -ld .mylogin.cnf -rw------- 1 root root 100 1月 21 14:48 .mylogin.cnf
而後在當前目錄下會生成一個文件 mylogin.cnf,而後再次執行時則mysql 後面跟要執行的命令便可;不會出現警告信息;
[root@localhost_002 ~]# mysql mysql -e "show tables"|sed '1'd|head -n3 columns_priv db event
4、假設一個網站使用cdn,全國各地有十多個cdn節點,須要你寫一個監控腳本,監控這些節點是否正常;
網址:www.baidu.com/index.php 源站IP 爲 61.135.169.125
網站經過CDN網絡在全國分別部署多個節點,讓用戶就近來訪問,提升用戶感知;能夠經過比較源站和各個節點的文件
思路:首先會把各個節點的IP地址都收集到一個ip.list的文件中; /tmp/ip.list
而後經過curl -x代理IP:port 網址 去訪問這個網站的首頁;並把信息保存到/tmp/source.txt curl -x 61.135.169.125:80 www.baidu.com/index.php
經過 for 循環 ip.list,而後用curl 命令來輸出網站首頁信息並保存在/tmp/ip.txt
再經過 diff 命令來比較這兩個文件是否相等,並寫入/tmp/$ip.diff; 或者用 md5sum grep -f 也能夠;
diff 1.txt 2.txt 若是這兩個文件相等,則不會任何輸出,不然則輸出不相等的內容;
再經過 wc -l /tmp/$ip.diff|awk '{print $1}' 判斷其行行數;
經過一個 if 來判斷,若是大於 0 ,則節點有異常; #由於默認是不會有任何輸出的;
[root@localhost_002 shell100]# cat /tmp/ip.list 61.135.169.125 [root@localhost_002 shell100]# cat 64.sh #!/bin/bash #這個腳本用來監控網站的CDN節點是否正常 #日期: 2019-01-21 #做者: yuanhh s_ip=61.135.169.125 url=www.baidu.com/index.php ipf=/tmp/ip.list curl -x$s_ip:80 $url 2>/dev/null > /tmp/source.txt for ip in `cat $ipf` do curl -x$ip:80 $url 2>/dev/null >/tmp/$ip.txt diff /tmp/source.txt /tmp/$ip.txt > /tmp/$ip.diff n=`wc -l /tmp/$ip.diff|awk '{print $1}'` if [ $n -gt 0 ] then echo "節點$ip有異常" fi done #執行命令:無輸出則表示節點正常; [root@localhost_002 shell100]# sh 64.sh #此時手動添加一個ip地址; [root@localhost_002 shell100]# cat /tmp/ip.list 61.135.169.125 127.0.0.1 [root@localhost_002 shell100]# sh 64.sh 節點127.0.0.1有異常 #有輸出,則表示節點有問題;
註釋:如上腳本,若是 cdn 節點有問題,則會有輸出,若是沒問題,則不會有任何輸出;
5、破解字符串,已知下面的字符串時經過 RADOM 隨機數 md5sum|cut -c 1-8 截取後的結果,請破解這些字符串對應的 md5sum 前用RANDOM 對應數字;
RANDOM 對應字符串:21029299 00205d1c a3da1677 1f6d12dd 890684ba
RANDOM 是一個數字,經過 md5sum 加密成字符串,而後截取前 8 個字符串;
[root@localhost_002 shell100]# echo $RANDOM 8451 [root@localhost_002 shell100]# echo $RANDOM|md5sum e7d303858c4ffc981a2d9c20850c8ec7 - [root@localhost_002 shell100]# echo $RANDOM|md5sum|cut -c 1-8 8d5ce504
思路:$RANDOM 的範圍值時 0-32767,要解這個題,須要遍歷0-32767全部數字,逐一和題目中給出的數字來匹配;
由於本地須要匹配5個字符串,因此須要逐一遍歷,那麼就有以下兩種方案:
以 0-32767 爲主,每遍歷一個數字,再遍歷五個字符串; #總共 32768 次; 每次 五個字符串;
以 5 個字符串爲主,每遍歷一個字符串,再遍歷32768個數字; #總共 5 * 32768 = 163840 次;5個字符串,每一個字符串遍歷 32768 次;
因爲要遍歷 32768 次,很耗時,因此選擇第一種方案,用 md5sum 每計算一個值,則和題目中的5個字符串對比;
方法一:首先用到了 for 來 遍歷 0 - 32767;而後分別打印出 每一個數字 md5sum 值; 定義變量 $n;
而後嵌入 for 來循環 這 五個字符串,定義變量 $c ;
而後用 if 判斷 "$n" == "$c" 若是等於則打印出來;
腳本以下:
[root@localhost_002 shell100]# cat 65.sh #!/bin/bash #這個腳本用來破解字符串 #日期: 2019-01-21 #做者: yuanhh #for n in {0..32767} 也能夠寫成這樣,同下面的用法相同; for n in `seq 0 32767` do md5=`echo $n|md5sum|cut -c 1-8` for c in 21029299 00205d1c a3da1677 1f6d12dd 890684ba do if [ "$md5" == "$c" ] then echo $n $c fi done done #執行:比較耗費資源; [root@localhost_002 shell100]# sh 65.sh 1346 00205d1c 7041 1f6d12dd 10082 890684ba 25345 a3da1677 25667 21029299
註釋:for n in {0..32767} ====== for n in `seq 0 32767` 用法是同樣子的;
方法二:先用 for 來遍歷 0-32767 個數字,而後計算每一個數字 md5sum 值前 8 位,並賦予變量 $m,而後把數字 echo $i $m 保存到/tmp/md5.txt裏;
而後嵌入文檔 EOF 字符串的內容保存到 當前目錄下 c.txt, 而後使用 grep -f 參數來比較;
註釋:由於 grep -f 參數會打印相同的部分,用在這裏最合適了;
[root@localhost_002 shell100]# cat 65.1.sh #!/bin/bash for i in `seq 0 32767` do m=`echo $i|md5sum|cut -c 1-8` echo $i $m done > /tmp/md5.txt cat > c.txt >>EOF 21029299 00205d1c a3da1677 1f6d12dd 890684ba EOF grep -f c.txt /tmp/md5.txt #執行腳本: 佔用系統資源多; [root@localhost_002 shell100]# sh 65.sh 1346 00205d1c 7041 1f6d12dd 10082 890684ba 25345 a3da1677 25667 21029299
註釋:嵌入文檔 EOF 格式:會在當前的腳本里在作寫入操做; 以 >>EOF 開頭,以 EOF 結尾;注意格式;
cat > c.txt >>EOF 21029299 00205d1c a3da1677 1f6d12dd 890684ba EOF