如何利用 tee 命令調(diào)試shell腳本中的管道
實(shí)例
下面是一個(gè)簡(jiǎn)單的腳本,腳本中 processid 函數(shù)的作用是查詢指定進(jìn)程名字的進(jìn)程ID,在管理linux服務(wù)器的過程中,這個(gè)是很常見的功能,processid 函數(shù)作用是利用多層管道命令查詢進(jìn)程ID,以下是測(cè)試腳本源碼
#!/bin/sh processid() { ipid=$(ps -ef | grep -w $1 | grep -v grep | awk '{print $2}') echo $ipid } case "$1" in i) processid $2 ;; *) echo "parameter error..$1" ;; esac
執(zhí)行腳本
我們執(zhí)行這個(gè)腳本查詢 zone9_log1 的進(jìn)程ID,下面是執(zhí)行的結(jié)果
[wanng@localhost ~]$ ./a.sh i zone9_log1 130530 144391 144392
為了和 zone9_log1 進(jìn)程實(shí)際的進(jìn)程ID對(duì)比,我們單獨(dú)執(zhí)行 ps -ef | grep -w zone9_log1 | grep -v grep | awk '{print $2}' 命令,執(zhí)行結(jié)果如下:
[wanng@localhost ~]$ ps -ef | grep -w zone9_log1 | grep -v grep | awk '{print $2}' 130530
問題
同樣的命令,確得到了不同的結(jié)果,我們?cè)谀_本中加入 tee 命令輸出管道的中間結(jié)果,調(diào)整之后的的腳本如下:
processid() { ipid=$(ps -ef | grep -w $1 | tee out1 | grep -v grep | tee out2 | awk '{print $2}') | tee out3 echo $ipid } case "$1" in i) processid $2 ;; *) echo "parameter error..$1" ;; esac
再次執(zhí)行腳本,本地會(huì)生成 out1 out2 out3 三個(gè)文件,記錄這管道命令的中間結(jié)果,下面是腳本執(zhí)行結(jié)果以及 out1 out2 out3 文件的內(nèi)容
[wang@localhost ~]$ ./a.sh i zone9_log1 130530 144885 144886 [wang@localhost ~]$ cat out1 wang 130530 1 0 4月24 pts/10 00:07:47 ./zone9_log1 ./zone9_log1.lua wang 144885 109338 0 20:45 pts/8 00:00:00 /bin/sh ./a.sh i zone9_log1 wang 144886 144885 0 20:45 pts/8 00:00:00 /bin/sh ./a.sh i zone9_log1 wang 144888 144886 0 20:45 pts/8 00:00:00 grep -w zone9_log1 [wang@localhost ~]$ cat out2 wang 130530 1 0 4月24 pts/10 00:07:47 ./zone9_log1 ./zone9_log1.lua wang 144885 109338 0 20:45 pts/8 00:00:00 /bin/sh ./a.sh i zone9_log1 wang 144886 144885 0 20:45 pts/8 00:00:00 /bin/sh ./a.sh i zone9_log1 [wang@localhost ~]$ cat out3 130530 144885 144886 [wang@localhost ~]$
原因
執(zhí)行腳本的時(shí)候,默認(rèn)會(huì)創(chuàng)建一個(gè)新的shell(也即一個(gè)新的進(jìn)程),上面的腳本 a.sh 就是在新的shell環(huán)境中執(zhí)行的。從上面的測(cè)試結(jié)果可以看出,ps -ef | grep -w zone9_log1 命令的結(jié)果中包含了執(zhí)行腳本身啟動(dòng)的進(jìn)程和我們要查詢的目標(biāo)進(jìn)程,我們只需要過濾掉腳本本身的進(jìn)程,就可以得到準(zhǔn)確的進(jìn)程ID,調(diào)整之后的腳本如下(暫時(shí)先保留 tee命令輸出的中間結(jié)果):
processid() { ipid=$(ps -ef | grep -w $1 | grep -v $0 | tee out1 | grep -v grep | tee out2 | awk '{print $2}') | tee out3 echo $ipid } case "$1" in i) processid $2 ;; *) echo "parameter error..$1" ;; esac
上面processid函數(shù)中 grep -v $0 作用是過濾掉腳本的名字,其中 $0 表示腳本的名字 ( a.sh )
驗(yàn)證
再次執(zhí)行腳本,結(jié)果如下:
[wanng@localhost ~]$ ./a.sh i zone9_log1 130530 [wanng@localhost ~]$ cat out1 wanng 130530 1 0 4月24 pts/10 00:07:51 ./zone9_log1 ./zone9_log1.lua wanng 146170 146168 0 21:11 pts/8 00:00:00 grep -w zone9_log1 [wanng@localhost ~]$ cat out2 wanng 130530 1 0 4月24 pts/10 00:07:51 ./zone9_log1 ./zone9_log1.lua [wanng@localhost ~]$ cat out3 130530
從上面的測(cè)試結(jié)果中看出,最后輸出的結(jié)果是正確的
總結(jié)
多層管道在shell腳本中是很常見的用法,使用起來也非常方便和高效的,但是腳本一旦出問題調(diào)試就會(huì)變得困難起來,合理的使用 tee 命令輸出管道的中間結(jié)果,可以快速的定位問題所在
以上就是如何利用 tee 命令調(diào)試shell腳本中的管道的詳細(xì)內(nèi)容,更多關(guān)于tee 命令調(diào)試shell腳本中的管道的資料請(qǐng)關(guān)注腳本之家其它相關(guān)文章!
相關(guān)文章
Shell腳本判斷進(jìn)程是否存在的實(shí)現(xiàn)示例
本文主要介紹了Shell腳本判斷進(jìn)程是否存在的實(shí)現(xiàn)示例,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2023-06-06Linux命令行和shell腳本編程寶典 Richard Blum
Linux命令行和shell腳本編程寶典,主要介紹了linux一些命令的使用2012-09-09shell腳本函數(shù):控制顏色、定位、居中顯示的代碼
這篇文章主要介紹了shell腳本函數(shù):控制顏色、定位、居中顯示的代碼,需要的朋友可以參考下2014-04-04Shell腳本傳遞參數(shù)的4種方式實(shí)例說明
Shell腳本是一種命令語言,可以用于自動(dòng)化執(zhí)行各種任務(wù),在腳本中,我們可以通過參數(shù)來傳遞信息,本文將介紹如何在shell腳本中傳遞參數(shù),包括位置參數(shù)、特殊變量、環(huán)境變量和命名參數(shù),需要的朋友可以參考下2023-06-06