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

下載本文檔

版權說明:本文檔由用戶提供并上傳,收益歸屬內容提供方,若內容存在侵權,請進行舉報或認領

文檔簡介

NSDS使用S數(shù)expect字符串截取及切使用S完成各種Linux運維任務時,一旦涉及到判斷、條件測試等相關操作時,往往需要對相關令輸出進行過濾,提取出符合要求的字符串。PPT根據課上的批量改名,編寫改進版renfilex.sh:能夠批量修改當前 件的擴展名,修改前/后的擴展名通過位置參數(shù)$1、$2提供${var:exprsubstr"$var"echo$var|cut-b 位置:dirname"字符串取文檔的基本名稱:basename只替換第一個匹配結果替換全部匹配結果從左向右,最短匹配刪除:${變量名#*從左向右,最長匹配刪除:${變量名##*從右向左,最短匹配刪除:${變量名%從右向左,最長匹配刪除:${變量名%%方法一,使用${}表達式[root@svr5~]#SCHOOL="TarenaIT[root@svr5~]#echo 6[root@svr5~]#echo[root@svr5~]#echo使用${}0(和數(shù)組下標編號類似)[root@svr5~]#echo ”應用示例:提取文件/etc/fstabMD5SUM[root@svr5~]#md5sum eef0254e6049a411dc30db864c0ee6ea[root@svr5~]#MD5STR=$(md5sum [root@svr5~]#echo expr格式:exprsubstr"$var"起始位置長度[root@svr5~]#echoTarenaIT[root@svr5~]#echoexprsubstr1${}相區(qū)分。SCHOOL6[root@svr5~]#exprsubstr"$SCHOOL"1[root@svr5~]#exprsubstr$SCHOOL1expr:[root@svr5~]#exprsubstr"$SCHOOL"11應用示例:還是提取文件/etc/fstabMD5SUMexpr[root@svr5~]#md5sum eef0254e6049a411dc30db864c0ee6ea[root@svr5~]#MD5STR=$(md5sum [root@svr5~]#exprsubstr"$MD5STR1 格式:echo$var|cutbb1(1expr),當省略結束位置時,視為截取到最后。[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應用示例:還是提取文件/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 引入變量來保存主機名以后,可以使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步驟四:編寫renfilex.sh1)驗證原始改名renfile.sh的效果 下的文件擴展名,將.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建立改進版$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字符串初值的處從鍵盤讀入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 步驟二:編寫sumx.sh,處理read輸入的初x1x(直接回車)時,為了避免執(zhí)行出錯,應為x1。1)編寫參考如[root@svr5~]#vimreadpx=${x:-i=1;while[$i-le$xletletecho1$x[root@svr5~]#od+xsumx.sh2)驗證、測試執(zhí)行效果:[root@svr5~]# [root@svr5~]# [root@svr5~]# 使用S數(shù)本案例要求編寫一個Sgetips.sh,相關要求如下IP當用戶輸入“EOF”IPADDSIP1格式1,整體賦值:數(shù)組名=(值1值 值2,獲取單個數(shù)組元素:${獲取所有數(shù)組元素:${數(shù)組名獲取數(shù)組元素個數(shù):${#數(shù)組名獲取連續(xù)的多個數(shù)組元素:${數(shù)組名[@]:獲取某個數(shù)組元素的長度:${#截取數(shù)組元素值的一部分:${#步驟一:認識數(shù)組的賦值/基本方在S環(huán)境中,變量默認均作為字符串處理,當參與expr、$[]數(shù)算時自動作為整數(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-//進入子S環(huán)echo//返回到原S環(huán)[root@svr5~]#declare+x [root@svr5~]# //進入子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 只讀變量除了不能修改值以外,也無法被unset清除(若要清除只讀變量,需要退出當前S程[root@svr5~]#unset-bash:unset:NM:cannotunset:readonly[root@svr5~]#echo[root@svr5~]#declare-a直接執(zhí)行declare-a,即不指定數(shù)組名參數(shù)時,會列出當前S環(huán)境中已定義的所有數(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時定義,也可以直接整體定義。整體賦值的格式為“組名=(值1值2值3 [root@svr5~]#MY_SVRS=(wwwftpmail[root@svr5~]#set|grep MY_SVRS=([0]="www"[1]="ftp"[2]="mail"S中的語法要求是比較松散的,所以我們也可以直接為單個數(shù)組元素賦值,格式為“數(shù)組名[下標]=值”,每個數(shù)組元素的編號(即下標)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因為需要讀多次,直到輸入“EOF”whileIP0內,下標的遞增通過一個變量i遇“EOF”while1IP[root@svr5~]#vim whilereadpIP(EOF)IP&&"$IP"EOF" letechoIPecho echo"總共包括${#IPADDS[@]}個地址 echo"其中第1個IP地址是 [root@svr5~]#od+xgetips.sh3)驗證、測試[root@svr5~]#720802總共包括6${數(shù)組名[@]}歷數(shù)組元素,通過連續(xù)取值的操作(或者%取模運算)IP(4個)驗證執(zhí)行效果expect提前準備好目標主機,IP用戶名為mike、執(zhí)行后自動登入,并且在目標主機建立測試文件expectTCL(FTP、SSH)自動定義環(huán)境變量:set創(chuàng)建交互式進程:spawn觸發(fā)預期交互:expect"預期會出現(xiàn)的文本:"{send"發(fā)送的文本\r"在spawn建立的進程中允許交互指令 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更改用戶mike的passwd:所有的驗證令牌已經成功更新步驟二:編寫expect_ssh,實現(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:”提示時,需要提供登錄用戶的;交互登錄成功以后,需要在“[mike@svr5~]$exit[root@svr5~]#ssh mike@'s Lastlogin:MonMay1112:02:392015from[mike@svr5~]$ Connectionto綜上所述,應該預先知道SSH連接目標主機的正常交互過程。需要在中來實現(xiàn)這些交互過程expectspawnsshexpect出現(xiàn)的提示文本、提供相應的文本,甚至進一步發(fā)送相應的子進程交互指令。其中用戶名、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:無法/tmp/mike.txt:沒有那個文件或執(zhí)行expect_ssh.sh自動登錄:[root@svr5~]#spawnsshmike@'sLastlogin:MonMay1112:08:472015frompwd>/tmp/mike.txt;[mike@svr5~]$pwd>/tmp/mike.txt;Connectionto再次檢查目標主機,會看到已經建立了/tmp/mike.txt文件,說明expect自動登錄并執(zhí)行命令[root@svr5~]#ls-l-rw-rw-r1mikemike1151112:17egrephttpd.confMACE-MailIP使用不帶-E選項的grep命令時,支持基本正則匹配模式。比如“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”開頭的行,這時[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選項-q表示quiet(靜默)的意思,結合此選項可以只做檢索而并不輸出,通常在內用來識別查找的目標是否存在,通過返回狀態(tài)$?來判斷,這樣可以忽略無關的文本信息,簡化輸出。比如,檢查/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)計本地用戶中登錄S為“/sbin/nologin”的用戶個數(shù)[root@svr5~]#egrepm10/sbin/nologin$'/etc/passwdftp:x:14:50:FTP[root@svr5~]#egrep-c'/sbin/nologin$' ccwcl統(tǒng)計使用“/bin/bash”作為登錄S的正常用戶個數(shù),可執(zhí)行:[root@svr5~]#egrep-c'/bin/bash$'[root@svr5~]#egrep'/bin/bash$'/etc/passwd|wc-基本元字符.——匹配任意單個字符[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)任意多次,也可以沒有。[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(頭或空幾個格再#),以及一些分隔的空行:[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,相關操作[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. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請下載最新的WinRAR軟件解壓。
  • 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請聯(lián)系上傳者。文件的所有權益歸上傳用戶所有。
  • 3. 本站RAR壓縮包中若帶圖紙,網頁內容里面會有圖紙預覽,若沒有圖紙預覽就沒有圖紙。
  • 4. 未經權益所有人同意不得將文件中的內容挪作商業(yè)或盈利用途。
  • 5. 人人文庫網僅提供信息存儲空間,僅對用戶上傳內容的表現(xiàn)方式做保護處理,對用戶上傳分享的文檔內容本身不做任何修改或編輯,并不能對任何下載內容負責。
  • 6. 下載文件中如有侵權或不適當內容,請與我們聯(lián)系,我們立即糾正。
  • 7. 本站不保證下載資源的準確性、安全性和完整性, 同時也不承擔用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。

評論

0/150

提交評論