linux案例第五天使用Shell完成各種Linux運(yùn)維任務(wù)時(shí)一旦涉及到判斷_第1頁(yè)
linux案例第五天使用Shell完成各種Linux運(yùn)維任務(wù)時(shí)一旦涉及到判斷_第2頁(yè)
linux案例第五天使用Shell完成各種Linux運(yùn)維任務(wù)時(shí)一旦涉及到判斷_第3頁(yè)
linux案例第五天使用Shell完成各種Linux運(yùn)維任務(wù)時(shí)一旦涉及到判斷_第4頁(yè)
linux案例第五天使用Shell完成各種Linux運(yùn)維任務(wù)時(shí)一旦涉及到判斷_第5頁(yè)
已閱讀5頁(yè),還剩23頁(yè)未讀, 繼續(xù)免費(fèi)閱讀

下載本文檔

版權(quán)說(shuō)明:本文檔由用戶(hù)提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請(qǐng)進(jìn)行舉報(bào)或認(rèn)領(lǐng)

文檔簡(jiǎn)介

NSDS使用S數(shù)expect字符串截取及切使用S完成各種Linux運(yùn)維任務(wù)時(shí),一旦涉及到判斷、條件測(cè)試等相關(guān)操作時(shí),往往需要對(duì)相關(guān)令輸出進(jìn)行過(guò)濾,提取出符合要求的字符串。PPT根據(jù)課上的批量改名,編寫(xiě)改進(jìn)版renfilex.sh:能夠批量修改當(dāng)前 件的擴(kuò)展名,修改前/后的擴(kuò)展名通過(guò)位置參數(shù)$1、$2提供${var:exprsubstr"$var"echo$var|cut-b 位置:dirname"字符串取文檔的基本名稱(chēng):basename只替換第一個(gè)匹配結(jié)果替換全部匹配結(jié)果從左向右,最短匹配刪除:${變量名#*從左向右,最長(zhǎng)匹配刪除:${變量名##*從右向左,最短匹配刪除:${變量名%從右向左,最長(zhǎng)匹配刪除:${變量名%%方法一,使用${}表達(dá)式[root@svr5~]#SCHOOL="TarenaIT[root@svr5~]#echo 6[root@svr5~]#echo[root@svr5~]#echo使用${}0(和數(shù)組下標(biāo)編號(hào)類(lèi)似)[root@svr5~]#echo ”應(yīng)用示例:提取文件/etc/fstabMD5SUM[root@svr5~]#md5sum eef0254e6049a411dc30db864c0ee6ea[root@svr5~]#MD5STR=$(md5sum [root@svr5~]#echo expr格式:exprsubstr"$var"起始位置長(zhǎng)度[root@svr5~]#echoTarenaIT[root@svr5~]#echoexprsubstr1${}相區(qū)分。SCHOOL6[root@svr5~]#exprsubstr"$SCHOOL"1[root@svr5~]#exprsubstr$SCHOOL1expr:[root@svr5~]#exprsubstr"$SCHOOL"11應(yīng)用示例:還是提取文件/etc/fstabMD5SUMexpr[root@svr5~]#md5sum eef0254e6049a411dc30db864c0ee6ea[root@svr5~]#MD5STR=$(md5sum [root@svr5~]#exprsubstr"$MD5STR1 格式:echo$var|cutbb1(1expr),當(dāng)省略結(jié)束位置時(shí),視為截取到最后。[root@svr5~]#echoTarenaIT[root@svr5~]#echo[root@svr5~]#echo$SCHOOL|cut-b1-[root@svr5~]#echo$SCHOOL|cut-b-[root@svr5~]#echo$SCHOOL|cut-b11-[root@svr5~]#echo$SCHOOL|cut-bIT[root@svr5~]#echo$SCHOOL|cut-bT[root@svr5~]#echo$SCHOOL|cut-bTarenaIT應(yīng)用示例:還是提取文件/etc/fstabMD5SUMcut[root@svr5~]#md5sum eef0254e6049a411dc30db864c0ee6ea[root@svr5~]#md5sum/etc/fstab|cut-b 1[root@svr5~]#echoTarenaIT1r[root@svr5~]#echoTaRRRRenaIT格式[root@svr5~]#echoTarenaIT[root@svr5~]#echoTaRRRRenaIT[root@svr5~]#echo [root@svr5~]#echo /local [root@svr5~]#echo${HOSTNAME/$HOSTNAME/localhost.local[root@svr5~]# [root@svr5~]#hostname [root@svr5~]# [root@svr5~]# [root@svr5~]#hostname [root@svr5~]#hostname 引入變量來(lái)保存主機(jī)名以后,可以使S具有更廣泛的適用性。[root@svr5~]#echo[root@svr5~]#echo[root@svr5~]#echo[root@svr5~]#echo [root@svr5~]#echot[root@svr5~]#echo${MAIL##*/}basename[root@svr5~]#basename格式:${變量名%*}[root@svr5~]#echo [root@svr5~]#echo[root@svr5~]#echo操作${MAIL%/*}的效果與使用dirname命令提 [root@svr5~]#dirname[root@svr5~]#echo root@svr5~]#echo[root@svr5~]#echo${MAIL%%/*}3.[root@svr5步驟四:編寫(xiě)renfilex.sh1)驗(yàn)證原始改名renfile.sh的效果 下的文件擴(kuò)展名,將.doc改為.txt。[root@svr5~]#vimforFILEinmv$FILE[root@svr5~]#od+x[root@svr5~]#mkdir[root@svr5~]#cd[root@svr5rendir]#touchfile1.docabcde.docxxyyzz.docother1.xls[root@svr5rendir]#abcde.docfile1.dockillbill.mp4other1.xls[root@svr5rendir]#[root@svr5rendir]#abcde.txtfile1.txtkillbill.mp4other1.xls建立改進(jìn)版$1、$2[root@svr5rendir]#cp../renfile.sh[root@svr5rendir]#vimforFILEinmv$FILE*.doc[root@svr5rendir]# abcde.txtfile1.txtkillbill.mp4other1.xls[root@svr5rendir]#../renfilex.sh.txt[root@svr5rendir]# abcde.docfile1.dockillbill.mp4other1.xls*.mp4[root@svr5rendir]# abcde.docfile1.dockillbill.mp4other1.xls[root@svr5rendir]#../renfilex.sh.mp4[root@svr5rendir]# abcde.docfile1.dockillbill.mkvother1.xls字符串初值的處從鍵盤(pán)讀入x1varNull$var“wordvar[root@svr5~]#echo TarenaIT[root@svr5~]#echo${SCHOOL:- TarenaIT[root@svr5~]#echo TarenaIT[root@svr5~]#unset [root@svr5~]#echo${SCHOOL:- [root@svr5~]#echo [root@svr5varNull$varword”,并將此字串賦值給變量var。[root@svr5~]#echo [root@svr5~]#echo [root@svr5~]#echo [root@svr5~]#echo [root@svr5~]#echo${SCHOOL:=TarenaITGroup[root@svr5~]#echo 步驟二:編寫(xiě)sumx.sh,處理read輸入的初x1x(直接回車(chē))時(shí),為了避免執(zhí)行出錯(cuò),應(yīng)為x1。1)編寫(xiě)參考如[root@svr5~]#vimreadpx=${x:-i=1;while[$i-le$xletletecho1$x[root@svr5~]#od+xsumx.sh2)驗(yàn)證、測(cè)試執(zhí)行效果:[root@svr5~]# [root@svr5~]# [root@svr5~]# 使用S數(shù)本案例要求編寫(xiě)一個(gè)Sgetips.sh,相關(guān)要求如下IP當(dāng)用戶(hù)輸入“EOF”IPADDSIP1格式1,整體賦值:數(shù)組名=(值1值 值2,獲取單個(gè)數(shù)組元素:${獲取所有數(shù)組元素:${數(shù)組名獲取數(shù)組元素個(gè)數(shù):${#數(shù)組名獲取連續(xù)的多個(gè)數(shù)組元素:${數(shù)組名[@]:獲取某個(gè)數(shù)組元素的長(zhǎng)度:${#截取數(shù)組元素值的一部分:${#步驟一:認(rèn)識(shí)數(shù)組的賦值/基本方在S環(huán)境中,變量默認(rèn)均作為字符串處理,當(dāng)參與expr、$[]數(shù)算時(shí)自動(dòng)作為整數(shù)處理。[root@svr5~]#[root@svr5~]#[root@svr5~]#echo [root@svr5~]#[root@svr5~]#echo [root@svr5~]#[root@svr5~]#echo [root@svr5~]#declare-i //先將A為整數(shù)變[root@svr5~]# [root@svr5~]#echo 3[root@svr5~]#declare-i [root@svr5~]#echodeclare-//進(jìn)入子S環(huán)echo//返回到原S環(huán)[root@svr5~]#declare+x [root@svr5~]# //進(jìn)入子S環(huán)[root@svr5~]#echo [root@svr5~]# //返回到原S環(huán)[root@svr5declarer[root@svr5~]#echo[root@svr5~]#declare-r [root@svr5~]#NM="TarenaIT -bash:NM:readonly[root@svr5~]#echo 只讀變量除了不能修改值以外,也無(wú)法被unset清除(若要清除只讀變量,需要退出當(dāng)前S程[root@svr5~]#unset-bash:unset:NM:cannotunset:readonly[root@svr5~]#echo[root@svr5~]#declare-a直接執(zhí)行declare-a,即不指定數(shù)組名參數(shù)時(shí),會(huì)列出當(dāng)前S環(huán)境中已定義的所有數(shù)組設(shè)置[root@svr5~]#declare- declare-adeclare-adeclare-adeclare-adeclare-arBASH_VERSINFO='([0]="3"[1]="2"[2]="25"[3]="1"declare-adeclare-a " declare- declare-adeclare-aPIPESTATUS='([0]="0"declare-aSVRS='([0]="www"[1]="ftp"[2]="mail"若要定義數(shù)組的成員,可以在declare時(shí)定義,也可以直接整體定義。整體賦值的格式為“組名=(值1值2值3 [root@svr5~]#MY_SVRS=(wwwftpmail[root@svr5~]#set|grep MY_SVRS=([0]="www"[1]="ftp"[2]="mail"S中的語(yǔ)法要求是比較松散的,所以我們也可以直接為單個(gè)數(shù)組元素賦值,格式為“數(shù)組名[下標(biāo)]=值”,每個(gè)數(shù)組元素的編號(hào)(即下標(biāo))03"""set|grep""3,直4[root@svr5~]#"[root@svr5~]#set|grep """ [root@svr5~]#echowwwftpmail[root@svr5~]#echo[root@svr5~]#echo [root@svr5~]#echo[root@svr5~]#echoftp[root@svr5~]#echo4[root@svr5~]#echo4getips.sh因?yàn)樾枰x多次,直到輸入“EOF”whileIP0內(nèi),下標(biāo)的遞增通過(guò)一個(gè)變量i遇“EOF”while1IP[root@svr5~]#vim whilereadpIP(EOF)IP&&"$IP"EOF" letechoIPecho echo"總共包括${#IPADDS[@]}個(gè)地址 echo"其中第1個(gè)IP地址是 [root@svr5~]#od+xgetips.sh3)驗(yàn)證、測(cè)試[root@svr5~]#720802總共包括6${數(shù)組名[@]}歷數(shù)組元素,通過(guò)連續(xù)取值的操作(或者%取模運(yùn)算)IP(4個(gè))驗(yàn)證執(zhí)行效果expect提前準(zhǔn)備好目標(biāo)主機(jī),IP用戶(hù)名為mike、執(zhí)行后自動(dòng)登入,并且在目標(biāo)主機(jī)建立測(cè)試文件expectTCL(FTP、SSH)自動(dòng)定義環(huán)境變量:set創(chuàng)建交互式進(jìn)程:spawn觸發(fā)預(yù)期交互:expect"預(yù)期會(huì)出現(xiàn)的文本:"{send"發(fā)送的文本\r"在spawn建立的進(jìn)程中允許交互指令 expectSSH1)expect[root@svr5~]#yumyinstall ..expect.x86_640:5-Dependencytcl.x86_641:8.5.7-[root@svr5~]#which 2)SSH[root@svr5~]#servicesshd停止sshd:sshd:[root@svr5~]#useradd[root@svr5~]# |passwd--stdin更改用戶(hù)mike的passwd:所有的驗(yàn)證令牌已經(jīng)成功更新步驟二:編寫(xiě)expect_ssh,實(shí)現(xiàn)免交互登SSH[root@svr5~]#ssh Theauthenticityofhost'()'can'tbeRSAkeyfingerprintisAreyousureyouwanttocontinueconnectingyes/no)? Warning:Permanentlyadded''(RSA)tothelistofknownmike@'s Lastlogin:ThuMay722:05:442015from[mike@svr5~]$ Connectionto 次交互發(fā)生在出現(xiàn)“....password:”提示時(shí),需要提供登錄用戶(hù)的;交互登錄成功以后,需要在“[mike@svr5~]$exit[root@svr5~]#ssh mike@'s Lastlogin:MonMay1112:02:392015from[mike@svr5~]$ Connectionto綜上所述,應(yīng)該預(yù)先知道SSH連接目標(biāo)主機(jī)的正常交互過(guò)程。需要在中來(lái)實(shí)現(xiàn)這些交互過(guò)程expectspawnsshexpect出現(xiàn)的提示文本、提供相應(yīng)的文本,甚至進(jìn)一步發(fā)送相應(yīng)的子進(jìn)程交互指令。其中用戶(hù)名、IPset[root@svr5~]#vimsethost setusersetpassword spawnssh expect"password:"send$password\r" expect"\[$user\@"{send"pwd>/tmp/$user.txt;exit\r" [root@svr5~]#od+x[root@svr5~]#lsls:無(wú)法/tmp/mike.txt:沒(méi)有那個(gè)文件或執(zhí)行expect_ssh.sh自動(dòng)登錄:[root@svr5~]#spawnsshmike@'sLastlogin:MonMay1112:08:472015frompwd>/tmp/mike.txt;[mike@svr5~]$pwd>/tmp/mike.txt;Connectionto再次檢查目標(biāo)主機(jī),會(huì)看到已經(jīng)建立了/tmp/mike.txt文件,說(shuō)明expect自動(dòng)登錄并執(zhí)行命令[root@svr5~]#ls-l-rw-rw-r1mikemike1151112:17egrephttpd.confMACE-MailIP使用不帶-E選項(xiàng)的grep命令時(shí),支持基本正則匹配模式。比如“word”檢索、“^word”匹word“word$word[root@svr5~]#grep'^r'rpc:x:32:32:PortmapperRPCrpcuser:x:29:29:RPCService[root@svr5~]#grep'localhost$'localhost.localgreproot”或者以“daemon”開(kāi)頭的行,這時(shí)[root@svr5~]#grep'^root|^daemon' [root@svr5[root@svr5~]#grep'^root\|^daemon grepEegrep|[root@svr5~]#grep-E'^root|^daemon'[root@svr5~]#egrep'^root|^daemon'grepEegrep選項(xiàng)-q表示quiet(靜默)的意思,結(jié)合此選項(xiàng)可以只做檢索而并不輸出,通常在內(nèi)用來(lái)識(shí)別查找的目標(biāo)是否存在,通過(guò)返回狀態(tài)$?來(lái)判斷,這樣可以忽略無(wú)關(guān)的文本信息,簡(jiǎn)化輸出。比如,檢查/etc/hostsYES”,否則[root@svr5~]#grep'^'/etc/hosts&&echo"YES"||echo [root@svr5~]#grep-q'^'/etc/hosts&&echo"YES"||echoq&>/dev/null^、$[root@svr5~]#egrep'^id'[root@svr5~]#egrep'^HOSTNAME'統(tǒng)計(jì)本地用戶(hù)中登錄S為“/sbin/nologin”的用戶(hù)個(gè)數(shù)[root@svr5~]#egrepm10/sbin/nologin$'/etc/passwdftp:x:14:50:FTP[root@svr5~]#egrep-c'/sbin/nologin$' ccwcl統(tǒng)計(jì)使用“/bin/bash”作為登錄S的正常用戶(hù)個(gè)數(shù),可執(zhí)行:[root@svr5~]#egrep-c'/bin/bash$'[root@svr5~]#egrep'/bin/bash$'/etc/passwd|wc-基本元字符.——匹配任意單個(gè)字符[root@svr5~]#cat##Thisscriptwillbeexecuted*after*alltheotherinit#Youcanputyourowninitializationstuffinhereifyou#wanttodothefullSysVstyleinitstuff.8.touch[root@svr5~]#egrep'.'##Thisscriptwillbeexecuted*after*alltheotherinit#Youcanputyourowninitializationstuffinhereifyou#wanttodothefullSysVstyleinittouch輸出/etc/rc.local(–v[root@svr5~]#egrep-v'.'/etc/rc.local3.[root@svr51.[root@svr5~]#egrep'^$'/etc/rc.local3.[root@svr55)*還以/etc/rc.local[root@svr5~]#cat##Thisscriptwillbeexecuted*after*alltheotherinit#Youcanputyourowninitializationstuffinhereifyou#wanttodothefullSysVstyleinitstuff.8.touch輸出包括[root@svr5~]#egrep'f+'#Thisscriptwillbeexecuted*after*alltheotherinit#Youcanputyourowninitializationstuffinhereifyou#wanttodothefullSysVstyleinit[root@svr5~]#egrep'init(ial)?'#Thisscriptwillbeexecuted*after*alltheotherinit#Youcanputyourowninitializationstuffinhereifyou#wanttodothefullSysVstyleinitstu、stuf、stuff、stufff、……的行,即末尾的“f”可出現(xiàn)任意多次,也可以沒(méi)有。[root@svr5~]#egrep'stuf*'#Youcanputyourowninitializationstuffinhereifyou#wanttodothefullSysVstyleinit[root@svr5~]#egrep'.*'##Thisscriptwillbeexecuted*after*alltheotherinit#Youcanputyourowninitializationstuffinhereifyou#wanttodothefullSysVstyleinitstuff.8.touch[root@svr5~]#egrep'^r.*nologin$'rpc:x:32:32:PortmapperRPCrpcuser:x:29:29:RPCService[root@svr5~]#vimabdefghidedefabababcabCD-TARENAITcdcdoabababab[root@svr5~]#egrep'(ab){3}'cdcdoabababab[root@svr5~]#egrep'(ab){2,4}'dedefababcdcdoabababab[root@svr5~]#egrep'(ab){3,}'cdcdoababababbracet.txt[root@svr5~]#catabdefghidedefabababcabCD-TARENAITcdcdoabababab[root@svr5~]#egrep'ab[cd]'abdefghiabcabCD-[root@svr5~]#egrep'[A-Z]'abcabCD-TARENAIToabababab[root@svr5~]#egrep'[^a-zA-Z]'abcabCD-[root@svr5~]#cat##Thisscriptwillbeexecuted*after*alltheotherinit#Youcanputyourowninitializationstuffinhereifyou#wanttodothefullSysVstyleinitstuff.8.touch[root@svr5~]#egrep'\binit\b'#Thisscriptwillbeexecuted*after*alltheotherinit#wanttodothefullSysVstyleinit[root@svr5~]#egrep'\<init\>'#Thisscriptwillbeexecuted*after*alltheotherinit#wanttodothefullSysVstyleinit輸出包括以“l(fā)l”[root@svr5~]#egrep'll\>'#Thisscriptwillbeexecuted*after*alltheotherinit#wanttodothefullSysVstyleinit[root@svr5~]#egrep'll\b'#Thisscriptwillbeexecuted*after*alltheotherinit#wanttodothefullSysVstyleinit[root@svr5~]#egrep'\<IDE\>|\<CDROM\>'UniformMulti-tformE-IDEdriverRevision:PIIX4:IDEcontrolleratPCIslotProbingIDEinterfaceProbingIDEinterfacehdc:VMwareVirtualIDECDROMDrive,ATAPICD/DVD-ROMProbingIDEinterface[root@svr5~]#egrep-i'eth|network|bluetooth'InitalizingnetworkdropmonitorBluetooth:CoreverBluetooth:HCIdeviceandconnectionmanagerBluetooth:HCIsocketlayerBluetooth:HCIUSBdriververIn(R)PRO/1000NetworkDriver-version7.3.21-k4-3-e1000:eth0:e1000_probe:In(R)PRO/1000NetworkRHEL6httpdhttpd.conf(頭或空幾個(gè)格再#),以及一些分隔的空行:[root@svr5~]#head/etc/httpd/conf/httpd.conf##ThisisthemainApacheserverconfigurationfile.Itcontains#configurationdirectivesthatgivetheserverits#See<URL:>fordetailed#Inparticular,#<URL:#foradiscussionofeachconfiguration###DoNOTsimplyreadtheinstructionsinherewithout[root@svr5~]#egrep-c".*"/etc/httpd/conf/httpd.conf13. [root@svr5~]#egrep-c"#" [root@svr5~]#egrep-c"^$" [root@svr5~]#egrep-c-v'#|^$'httpd.confhttpd.conf.min,相關(guān)操作[root@svr5~]#egrep-v'#|^$'/etc/httpd/conf/httpd.conf>[root@svr5~]#head ServerTokensServerRootPidFileTimeoutKeepAliveMaxKeepAliveRequestsKeepAliveTimeout<IfModuleStartServersMinSpareServersifconfig,MAC62[root@svr5~]#ifconfigeth0Linkencap:EthernetHWaddrinetaddr:Bcast:55inet6addr:fe80::20c:29ff:fe82:9e9/64UPBROADCASTRUNNINGMULTICASTMTU:1500RXpackets:177666errors:0dropped:0overruns:0TXpackets:101720errors:0dropped:0overruns:0collisions:0RX (17.5MiB)TX (12.8其中,[0-9a-fA-F]{25}5[root@svr5~]#ifcon

溫馨提示

  • 1. 本站所有資源如無(wú)特殊說(shuō)明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請(qǐng)下載最新的WinRAR軟件解壓。
  • 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請(qǐng)聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶(hù)所有。
  • 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁(yè)內(nèi)容里面會(huì)有圖紙預(yù)覽,若沒(méi)有圖紙預(yù)覽就沒(méi)有圖紙。
  • 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
  • 5. 人人文庫(kù)網(wǎng)僅提供信息存儲(chǔ)空間,僅對(duì)用戶(hù)上傳內(nèi)容的表現(xiàn)方式做保護(hù)處理,對(duì)用戶(hù)上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對(duì)任何下載內(nèi)容負(fù)責(zé)。
  • 6. 下載文件中如有侵權(quán)或不適當(dāng)內(nèi)容,請(qǐng)與我們聯(lián)系,我們立即糾正。
  • 7. 本站不保證下載資源的準(zhǔn)確性、安全性和完整性, 同時(shí)也不承擔(dān)用戶(hù)因使用這些下載資源對(duì)自己和他人造成任何形式的傷害或損失。

評(píng)論

0/150

提交評(píng)論