04: 字串處理 擴充套件的指令碼技巧 正則表示式
NSD SHELL DAY04
1 案例1:字串擷取及切割
1.1 問題
使用Shell完成各種Linux運維任務時,一旦涉及到判斷、條件測試等相關操作時,往往需要對相關的命令輸出進行過濾,提取出符合要求的字串。
本案例要求熟悉字串的常見處理操作,完成以下任務練習:
- 參考PPT示範操作,完成子串擷取、替換等操作
- 根據課上的批量改名指令碼,編寫改進版renfilex.sh:能夠批量修改當前目錄下所有檔案的副檔名,修改前/後的副檔名通過位置引數$1、$2提供
1.2 方案
子串擷取的三種用法:
- ${變數名:起始位置:長度}
- expr substr "$變數名" 起始位置 長度
- echo $變數名 | cut -b 起始位置-結束位置
子串替換的兩種用法:
- 只替換第一個匹配結果:${變數名/old/new}
- 替換全部匹配結果:${變數名//old/new}
字串掐頭去尾:
- 從左向右,最短匹配刪除:${變數名#*關鍵詞}
- 從左向右,最長匹配刪除:${變數名##*關鍵詞}
- 從右向左,最短匹配刪除:${變數名%關鍵詞*}
- 從右向左,最長匹配刪除:${變數名%%關鍵詞*}
1.3 步驟
實現此案例需要按照如下步驟進行。
步驟一:字串的擷取
1)方法一,使用 ${}表示式
格式:${變數名:起始位置:長度}
使用${}方式擷取字串時,起始位置是從0開始的。
定義一個變數phone,並確認其字串長度:
- [[email protected] ~]# phone="13788768897"
- [[email protected] ~]# echo ${#phone}
- 11 //包括11個字元
使用${}擷取時,起始位置可以省略,省略時從第一個字元開始截。比如,以下操作都可以從左側開始擷取前6個字元:
- [[email protected] ~]# echo ${phone:0:6}
- 137887
或者
- [[email protected] ~]# echo ${phone::6}
- 137887
因此,如果從起始位置1開始擷取6個字元,那就變成這個樣子了:
- [[email protected] ~]# echo ${phone:1:6}
- 378876
2)方法二,使用 expr substr
格式:expr substr "$變數名" 起始位置 長度
還以前面的phone變數為例,確認原始值:
- [[email protected] ~]# echo $phone
- 13788768897
使用expr substr擷取字串時,起始編號從1開始,這個要注意與${}相區分。
從左側擷取phone變數的前6個字元:
- [[email protected] ~]# expr substr "$phone" 1 6
- 137887
從左側擷取phone變數,從第9個字元開始,擷取3個字元:
- [[email protected] ~]# expr substr "$phone" 9 3
- 897
3)方式三,使用cut分割工具
格式:echo $變數名 | cut -b 起始位置-結束位置
選項 -b 表示按位元組擷取字元,其中起始位置、結束位置都可以省略。當省略起始位置時,視為從第1個字元開始(編號也是從1開始,與expr類似),當省略結束位置時,視為擷取到最後。
還以前面的Phone變數為例,確認原始值:
- [[email protected] ~]# echo $phone
- 13788768897
從左側擷取前6個字元,可執行以下操作:
- [[email protected] ~]# echo $phone | cut -b 1-6
- 137887
從第8個字元擷取到末尾:
- [[email protected] ~]# echo $phone | cut -b 8-
- 8897
只擷取單個字元,比如第9個字元:
- [[email protected] ~]# echo $phone | cut -b 9
- 8
擷取不連續的字元,比如第3、5、8個字元:
- [[email protected] ~]# echo $phone | cut -b 3,5,8
- 788
4)一個隨機密碼的案例
版本1:
- [[email protected] ~]# vim rand.sh
- #!/bin/bash
- x=abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789
- //所有密碼的可能性是26+26+10=62(0-61是62個數字)
- num=$[RANDOM%62]
- pass=${x:num:1}
版本2:
- [[email protected] ~]# vim rand.sh
- #!/bin/bash
- x=abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789
- //所有密碼的可能性是26+26+10=62(0-61是62個數字)
- pass=''
- for i in {1..8}
- do
- num=$[RANDOM%62]
- tmp=${x:num:1}
- pass=${pass}$tmp
- done
- echo $pass
步驟二:字串的替換
1)只替換第1個子串
格式:${變數名/old/new}
還以前面的phone變數為例,確認原始值:
- [[email protected] ~]# echo $phone
- 13788768897
將字串中的第1個8替換為X:
- [[email protected] ~]# echo ${phone/8/X}
- 137X8768897
2)替換全部子串
格式:${變數名//old/new}
將phone字串中的所有8都替換為X:
- [[email protected] ~]# echo ${phone//8/X}
- 137XX76XX97
步驟三:字串的匹配刪除
以處理系統預設的賬戶資訊為例,定義變數A:
- [[email protected] ~]# A=`head -1 /etc/passwd`
- [[email protected] ~]# echo $A
- root:x:0:0:root:/root:/bin/bash
1)從左向右,最短匹配刪除
格式:${變數名#*關鍵詞}
刪除從左側第1個字元到最近的關鍵詞“:”的部分,* 作萬用字元理解:
- [[email protected] ~]# echo ${A#*:}
- x:0:0:root:/root:/bin/bash
2)從左向右,最長匹配刪除
格式:${變數名##*關鍵詞}
刪除從左側第1個字元到最遠的關鍵詞“:”的部分:
- [[email protected] ~]# echo $A //確認變數A的值
- root:x:0:0:root:/root:/bin/bash
- [[email protected] ~]# echo ${A##*:}
- /bin/bash
3)從右向左,最短匹配刪除
格式:${變數名%關鍵詞*}
刪除從右側最後1個字元到往左最近的關鍵詞“:”的部分,* 做萬用字元理解:
- [[email protected] ~]# echo ${A%:*}
- root:x:0:0:root:/root
4)從右向左,最長匹配刪除
格式:${變數名%%關鍵詞*}
刪除從右側最後1個字元到往左最遠的關鍵詞“:”的部分:
- [[email protected] ~]# echo ${A%%:*}
- root
步驟四:編寫renfilex.sh指令碼
建立一個測試用的測試檔案
- [[email protected] ~]# mkdir rendir
- [[email protected] ~]# cd rendir
- [[email protected] rendir]# touch {a,b,c,d,e,f,g,h,i}.doc
- [[email protected] rendir]# ls
- a.doc b.doc c.doc d.doc e.doc f.doc g.doc h.doc i.doc
1)批量修改副檔名的指令碼
指令碼用途為:批量修改當前目錄下的副檔名,將.doc改為.txt。
指令碼內容參考如下:
- [[email protected] rendir]# vim renfile.sh
- #!/bin/bash
- for i in `ls *.doc` #注意這裡有反引號
- do
- mv $i ${i%.*}.txt
- done
- [[email protected] ~]# chmod +x renfile.sh
測試指令碼:
- [[email protected] rendir]# ./renfile.sh
- [[email protected] rendir]# ls
- a.txt b.txt c.txt d.txt e.txt f.txt g.txt h.txt i.txt
2)改進版指令碼(批量修改副檔名)
通過位置變數 $1、$2提供更靈活的指令碼,改進的指令碼編寫參考如下:
- [[email protected] rendir]# vim ./renfile.sh
- #!/bin/bash
- #version:2
- for i in `ls *.$1`
- do
- mv $i ${i%.*}.$2
- done
3)驗證、測試改進後的指令碼
將 *.doc檔案的副檔名改為.txt:
- [[email protected] rendir]# ./renfile.sh txt doc
將 *.doc檔案的副檔名改為.mp4:
- [[email protected] rendir]# ./renfile.sh doc mp4
2 案例2:字串初值的處理
2.1 問題
本案例要求編寫一個指令碼sumx.sh,求從1-x的和,相關要求如下:
- 從鍵盤讀入x值
- 當用戶未輸入任何值時,預設按1計算
2.2 方案
通過${var:-word}判斷變數是否存在,決定變數的初始值。
2.3 步驟
實現此案例需要按照如下步驟進行。
步驟一:認識字串初值的最常見處理方法
1)只取值,${var:-word}
若變數var已存在且非Null,則返回 $var 的值;否則返回字串“word”,原變數var的值不受影響。
變數值已存在的情況:
- [[email protected] ~]# XX=11
- [[email protected] ~]# echo $XX //檢視原變數值
- 11
- [[email protected] ~]# echo ${XX:-123} //因XX已存在,輸出變數XX的值
- 11
變數值不存在的情況:
- [[email protected] ~]# echo ${YY:-123} //因YY不存在,輸出“123”
- 123
編寫一個驗證知識點的參考示例指令碼如下:
- [[email protected] ~]# cat /root/test.sh
- #!/bin/bash
- read -p "請輸入使用者名稱:" user
- [ -z $user ] && exit //如果無使用者名稱,則指令碼退出
- read -p "請輸入密碼:" pass
- pass=${pass:-123456} //如果使用者沒有輸入密碼,則預設密碼為123456
- useradd $user
- echo "$pass" | passwd --stdin $user
步驟二:編寫sumx.sh指令碼,處理read輸入的初值
用來從鍵盤讀入一個正整數x,求從1到x的和;當用戶未輸入值(直接回車)時,為了避免執行出錯,應為x賦初值1 。
1)指令碼編寫參考如下
- [[email protected] ~]# vim sumx.sh
- #!/bin/bash
- read -p "請輸入一個正整數:" x
- x=${x:-1}
- i=1; SUM=0
- while [ $i -le $x ]
- do
- let SUM+=i
- let i++
- done
- echo "從1到$x的總和是:$SUM"
- [[email protected] ~]# chmod +x sumx.sh
2)驗證、測試指令碼執行效果:
- [[email protected] ~]# ./sumx.sh
- 請輸入一個正整數:25 //輸入25,正常讀入並計算、輸出結果
- 從1到25的總和是:325
- [[email protected] ~]# ./sumx.sh
- 請輸入一個正整數:70 //輸入70,正常讀入並計算、輸出結果
- 從1到70的總和是:2485
- [[email protected] ~]# ./sumx.sh
- 請輸入一個正整數: //直接回車,設x=1後計算、輸出結果
- 從1到1的總和是:1
3 案例3:expect預期互動
3.1 問題
本案例要求編寫一個expect指令碼,實現SSH登入的自動互動:
- 提前準備好目標主機,IP地址為192.168.4.5
- 執行指令碼後自動登入,並且在目標主機建立測試檔案 /tmp/mike.txt
3.2 方案
expect可以為互動式過程(比如FTP、SSH等登入過程)自動輸送預先準備的文字或指令,而無需人工干預。觸發的依據是預期會出現的特徵提示文字。
儲備知識(傳送郵件的幾種方式):
- [[email protected] ~]# echo "test mail" | mail -s test root
- [[email protected] ~]# mail -s test root < /etc/passwd
- [[email protected] ~]# mail -s test root << EOF
- test mail
- hell world
- EOF
3.3 步驟
實現此案例需要按照如下步驟進行。
步驟一:準備expect及SSH測試環境
1)安裝expect工具
- [[email protected] ~]# yum -y install expect //安裝expect
- .. ..
- Installed:
- expect.x86_64 0:5.44.1.15-5.el6_4
- Dependency Installed:
- tcl.x86_64 1:8.5.7-6.el6
- [[email protected] ~]# which expect //確認expect路徑
- /usr/bin/expect
步驟二:編寫指令碼,實現免互動登入
1)任務需求及思路分析
在SSH登入過程中,如果是第一次連線到該目標主機,則首先會被要求接受金鑰,然後才提示輸入密碼:
注意:不要照抄這裡的IP地址,需要根據自己的實際IP填寫!!!
- [[email protected] ~]# ssh [email protected] //連線目標主機
- The authenticity of host '192.168.4.5 (192.168.4.5)' can't be established.
- RSA key fingerprint is 58:a0:d6:00:c7:f1:34:5d:6c:6d:70:ce:e0:20:f8:f3.
- Are you sure you want to continue connecting (yes/no)? yes //接受金鑰
- Warning: Permanently added '192.168.4.5' (RSA) to the list of known hosts.
- [email protected]'s password: //驗證密碼
- Last login: Thu May 7 22:05:44 2015 from 192.168.4.5
- [[email protected] ~]$ exit //返回客戶端
- logout
- Connection to 192.168.4.5 closed.
當然,如果SSH登入並不是第一次,則接受金鑰的環節就沒有了,而是直接進入驗證密碼的過程:
注意:不要照抄這裡的IP地址,需要根據自己的實際IP填寫!!!
- [[email protected] ~]# ssh [email protected] //連線目標主機
- [email protected]'s password: //驗證密碼
- Last login: Mon May 11 12:02:39 2015 from 192.168.4.5
- [[email protected] ~]$ exit //返回客戶端
- logout
- Connection to 192.168.4.5 closed.
2)根據實現思路編寫指令碼檔案
指令碼內容參考如下版本1:
注意:不要照抄腳本里的IP地址與密碼,需要根據自己的實際情況填寫!!!
- [[email protected] ~]# vim expect_ssh.sh
- #!/bin/bash
- expect << EOF
- spawn ssh 192.168.4.5 #//建立互動式程序
- expect "password:" { send "123456\r" } #//自動傳送密碼
- expect "#" { send "touch /tmp.txt\r" } #//傳送命令
- expect "#" { send "exit\r" }
- EOF
- [[email protected] ~]# chmod +x expect_ssh.sh
通過迴圈批量操作,版本2:
注意:不要照抄腳本里的IP地址與密碼,需要根據自己的實際情況填寫!!!
- [[email protected] ~]# vim expect_ssh.sh
- #!/bin/bash
- for i in 10 11 #注意IP根據實際情況填寫
- do
- expect << EOF
- spawn ssh 192.168.4.$i #//建立互動式程序
- expect "password:" { send "123456\r" } #//自動傳送密碼
- expect "#" { send "touch /tmp.txt\r" } #//傳送命令
- expect "#" { send "exit\r" }
- EOF
- done
- [[email protected] ~]# chmod +x expect_ssh.sh
注意事項:
expect指令碼的最後一行預設不執行
如果不希望ssh時出現yes/no的提示,遠端時使用如下選項:
# ssh -o StrictHostKeyChecking=no server0
4 案例4:使用正則表示式
4.1 問題
本案例要求熟悉正則表示式的編寫,完成以下任務:
- 利用egrep工具練習正則表示式的基本用法
4.2 方案
表-1 基本正則列表
表-1 擴充套件正則列表
4.3 步驟
實現此案例需要按照如下步驟進行。
步驟一:正則表示式匹配練習
1)典型的應用場合:grep、egrep檢索文字行
grep命令不帶-E選項時,支援基本正則匹配模式。比如“word”關鍵詞檢索、“^word”匹配以word開頭的行、“word$”匹配以word結尾的行……等等。
輸出以“r”開頭的使用者記錄:
- [[email protected] ~]# grep '^r' /etc/passwd
- root:x:0:0:root:/root:/bin/bash
- rpc:x:32:32:Portmapper RPC user:/:/sbin/nologin
- rpcuser:x:29:29:RPC Service User:/var/lib/nfs:/sbin/nologin
輸出以“localhost”結尾的行:
- [[email protected] ~]# grep 'localhost$' /etc/hosts
- 127.0.0.1 localhost.localdomain localhost
若希望在grep檢索式同時組合多個條件,比如輸出以“root”或者以“daemon”開頭的行:
- [[email protected] ~]# grep '^root|^daemon' /etc/passwd //搜尋無結果
- [[email protected] ~]#
而若若使用grep -E或egrep命令,可支援擴充套件正則匹配模式,能夠自動識別 |、{} 等擴充套件正則表示式中的特殊字元,用起來更加方便,比如:
- [[email protected] ~]# grep -E '^(root|daemon)' /etc/passwd
- root:x:0:0:root:/root:/bin/bash
- daemon:x:2:2:daemon:/sbin:/sbin/nologin
或者
- [[email protected] ~]# egrep '^(root|daemon)' /etc/passwd
- root:x:0:0:root:/root:/bin/bash
- daemon:x:2:2:daemon:/sbin:/sbin/nologin
使用grep -E 與 使用egrep命令完全等效,推薦使用後者,特別是涉及到複雜的正則表示式的時候。
2)grep、egrep命令的-q選項
選項 -q 表示 quiet(靜默)的意思,結合此選項可以只做檢索而並不輸出,通常在指令碼內用來識別查詢的目標是否存在,通過返回狀態 $? 來判斷,這樣可以忽略無關的文字資訊,簡化指令碼輸出。
比如,檢查/etc/hosts檔案內是否存在192.168.4.4的對映記錄,如果存在則顯示“YES”,否則輸出“NO”,一般會執行:
- [[email protected] ~]# grep '^192.168.4.4' /etc/hosts && echo "YES" || echo "NO"
- 192.168.4.4 svr5.tarena.com svr5
- YES
這樣grep的輸出資訊和指令碼判斷後的提示混雜在一起,使用者不易辨別,所以可以改成以下操作:
- [[email protected] ~]# grep -q '^192.168.4.4' /etc/hosts && echo "YES" || echo "NO"
- YES
是不是清爽多了,從上述結果也可以看到,使用 -q 選項的效果與使用 &> /dev/null的效果類似。
3)基本元字元 ^、$ —— 匹配行首、行尾
輸出註釋的配置行(以#開頭的行):
- [[email protected] ~]# egrep '^#' /etc/inittab
統計本地使用者中登入Shell為“/sbin/nologin”的使用者個數:
提示:-m10僅在檔案的前10行中過濾,後面的行不再過濾。
- [[email protected] ~]# egrep -m10 '/sbin/nologin$' /etc/passwd //先確認匹配正確
- bin:x:1:1:bin:/bin:/sbin/nologin
- daemon:x:2:2:daemon:/sbin:/sbin/nologin
- adm:x:3:4:adm:/var/adm:/sbin/nologin
- lp:x:4:7:lp:/var/spool/lpd:/sbin/nologin
- mail:x:8:12:mail:/var/spool/mail:/sbin/nologin
- uucp:x:10:14:uucp:/var/spool/uucp:/sbin/nologin
- operator:x:11:0:operator:/root:/sbin/nologin
- games:x:12:100:games:/usr/games:/sbin/nologin
- gopher:x:13:30:gopher:/var/gopher:/sbin/nologin
- ftp:x:14:50:FTP User:/var/ftp:/sbin/nologin
- [[email protected] ~]# egrep -c '/sbin/nologin$' /etc/passwd
- 32 //結合 -c 選項輸出匹配的行數
使用 -c 選項可輸出匹配行數,這與通過管道再 wc -l的效果是相同的,但是寫法更簡便。比如,統計使用“/bin/bash”作為登入Shell的正常使用者個數,可執行:
- [[email protected] ~]# egrep -c '/bin/bash$' /etc/passwd
- 26
或者
- [[email protected] ~]# egrep '/bin/bash$' /etc/passwd | wc -l
- 26
4)基本元字元 . —— 匹配任意單個字元
以/etc/rc.local檔案為例,確認文字內容:
- [[email protected] ~]# cat /etc/rc.local
- #!/bin/sh
- #
- # This script will be executed *after* all the other init scripts.
- # You can put your own initialization stuff in here if you don't
- # want to do the full Sys V style init stuff.
- touch /var/lock/subsys/local
輸出/etc/rc.local檔案內至少包括一個字元(\n換行符除外)的行,即非空行:
- [[email protected] ~]# egrep '.' /etc/rc.local
- #!/bin/sh
- #
- # This script will be executed *after* all the other init scripts.
- # You can put your own initialization stuff in here if you don't
- # want to do the full Sys V style init stuff.
- touch /var/lock/subsys/local
輸出/etc/rc.local檔案內的空行(用 –v 選項將條件取反):
- [[email protected] ~]# egrep -v '.' /etc/rc.local
- [[email protected] ~]#
上述取空行的操作與下列操作效果相同:
- [[email protected] ~]# egrep '^$' /etc/rc.local
- [[email protected] ~]#
5)基本元字元 +、?、* —— 目標出現的次數
還以/etc/rc.local檔案為例:
- [[email protected] ~]# cat /etc/rc.local
- #!/bin/sh
- #
- # This script will be executed *after* all the other init scripts.
- # You can put your own initialization stuff in here if you don't
- # want to do the full Sys V style init stuff.
- touch /var/lock/subsys/local
輸出包括 f、ff、ff、……的行,即“f”至少出現一次:
- [[email protected] ~]# egrep 'f+' /etc/rc.local
- # This script will be executed *after* all the other init scripts.
- # You can put your own initialization stuff in here if you don't
- # want to do the full Sys V style init stuff.
輸出包括init、initial的行,即末尾的“ial”最多出現一次(可能沒有):
- [[email protected] ~]# egrep 'init(ial)?' /etc/rc.local
- # This script will be executed *after* all the other init scripts.
- # You can put your own initialization stuff in here if you don't
- # want to do the full Sys V style init stuff.
輸出包括stu、stuf、stuff、stufff、……的行,即末尾的“f”可出現任意多次,也可以沒有。重複目標只有一個字元時,可以不使用括號:
- [[email protected] ~]# egrep 'stuf*' /etc/rc.local
- # You can put your own initialization stuff in here if you don't
- # want to do the full Sys V style init stuff.
輸出所有行,單獨的“.*”可匹配任意行(包括空行):
- [[email protected] ~]# egrep '.*' /etc/rc.local
- #!/bin/sh
- #
- # This script will be executed *after* all the other init scripts.
- # You can put your own initialization stuff in here if you don't
- # want to do the full Sys V style init stuff.
- touch /var/lock/subsys/local
輸出/etc/passwd檔案內“r”開頭且以“nologin”結尾的使用者記錄,即中間可以是任意字元:
- [[email protected] ~]# egrep '^r.*nologin$' /etc/passwd
- rpc:x:32:32:Portmapper RPC user:/:/sbin/nologin
- rpcuser:x:29:29:RPC Service User:/var/lib/nfs:/sbin/nologin
6)元字元 {} —— 限定出現的次數範圍
建立一個練習用的測試檔案:
- [[email protected] ~]# vim brace.txt
- ab def ghi abdr
- dedef abab ghighi
- abcab CD-ROM
- TARENA IT GROUP
- cdcd ababab
- Hello abababab World
輸出包括ababab的行,即“ab”連續出現3次:
- [[email protected] ~]# egrep '(ab){3}' brace.txt
- cdcd ababab
- Hello abababab World
輸出包括abab、ababab、abababab的行,即“ab”連續出現2~4次:
- [[email protected] ~]# egrep '(ab){2,4}' brace.txt
- dedef abab ghighi
- cdcd ababab
- Hello abababab World
輸出包括ababab、abababab、……的行,即“ab”最少連續出現3次:
- [[email protected] ~]# egrep '(ab){3,}' brace.txt
- cdcd ababab
- Hello abababab World
7)元字元 [] —— 匹配範圍內的單個字元
還以前面的測試檔案bracet.txt為例:
- [[email protected] ~]# cat brace.txt
- ab def ghi abdr
- dedef abab ghighi
- abcab CD-ROM
- TARENA IT GROUP
- cdcd ababab
- Hello abababab World
輸出包括abc、abd的行,即前兩個字元為“ab”,第三個字元只要是c、d中的一個就符合條件:
- [[email protected] ~]# egrep 'ab[cd]' brace.txt
- ab def ghi abdr
- abcab CD-ROM
輸出包括大寫字母的行,使用[A-Z]匹配連續範圍:
- [[email protected] ~]# egrep '[A-Z]' brace.txt
- abcab CD-ROM
- TARENA IT GROUP
- Hello abababab World
過濾“非小寫字母”的其他字元:
- [[email protected] ~]# egrep '[^a-z]' brace.txt
8)單詞邊界匹配
以檔案/etc/rc.local為例:
- [[email protected] ~]# cat /etc/rc.local
- #!/bin/sh
- #
- # This script will be executed *after* all the other init scripts.
- # You can put your own initialization stuff in here if you don't
- # want to do the full Sys V style init stuff.
- touch /var/lock/subsys/local
輸出包括單詞“init”的行,檔案中“initialization”不合要求:
- [[email protected] ~]# egrep '\binit\b' /etc/rc.local
- # This script will be executed *after* all the other init scripts.
- # want to do the full Sys V style init stuff.
或者:
- [[email protected] ~]# egrep '\<init\>' /etc/rc.local
- # This script will be executed *after* all the other init scripts.
- # want to do the full Sys V style init stuff.
輸出包括以“ll”結尾的單詞的行,使用 \> 匹配單詞右邊界:
- [[email protected] ~]# egrep 'll\>' /etc/rc.local
- # This script will be executed *after* all the other init scripts.
- # want to do the full Sys V style init stuff.
或者:
- [[email protected] ~]# egrep 'll\b' /etc/rc.local
- # This script will be executed *after* all the other init scripts.
- # want to do the full Sys V style init stuff.
9)多個條件的組合
通過dmesg啟動日誌檢視藍芽裝置、網絡卡裝置相關的資訊:
- [[email protected] ~]# egrep -i 'eth|network|bluetooth' /var/log/dmesg
- Initalizing network drop monitor service
- Bluetooth: Core ver 2.10
- Bluetooth: HCI device and connection manager initialized
- Bluetooth: HCI socket layer initialized
- Bluetooth: HCI USB driver ver 2.9
- Intel(R) PRO/1000 Network Driver - version 7.3.21-k4-3-NAPI
- e1000: eth0: e1000_probe: Intel(R) PRO/1000 Network Connection