婷婷综合国产,91蜜桃婷婷狠狠久久综合9色 ,九九九九九精品,国产综合av

主頁 > 知識庫 > 如何利用 tee 命令調試shell腳本中的管道

如何利用 tee 命令調試shell腳本中的管道

熱門標簽:地圖標注審核周期 黑暗之魂3地圖標注 電話機器人對家居行業幫助大嗎 電商外呼系統排名 蘭州電銷機器人加盟 沈陽ai電銷智能機器人 AI智能電銷機器人壞處 合肥電銷外呼系統供應商 如何申請400的電話呀

實例

下面是一個簡單的腳本,腳本中 processid 函數的作用是查詢指定進程名字的進程ID,在管理linux服務器的過程中,這個是很常見的功能,processid 函數作用是利用多層管道命令查詢進程ID,以下是測試腳本源碼

#!/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

執行腳本

我們執行這個腳本查詢 zone9_log1 的進程ID,下面是執行的結果

[wanng@localhost ~]$ ./a.sh i zone9_log1
130530 144391 144392

為了和 zone9_log1 進程實際的進程ID對比,我們單獨執行 ps -ef | grep -w zone9_log1 | grep -v grep | awk '{print $2}' 命令,執行結果如下:

[wanng@localhost ~]$ ps -ef | grep -w zone9_log1 | grep -v grep | awk '{print $2}'
130530

問題

同樣的命令,確得到了不同的結果,我們在腳本中加入 tee 命令輸出管道的中間結果,調整之后的的腳本如下:

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

再次執行腳本,本地會生成 out1 out2 out3 三個文件,記錄這管道命令的中間結果,下面是腳本執行結果以及 out1 out2 out3 文件的內容

[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 ~]$ 

原因

執行腳本的時候,默認會創建一個新的shell(也即一個新的進程),上面的腳本 a.sh 就是在新的shell環境中執行的。從上面的測試結果可以看出,ps -ef | grep -w zone9_log1 命令的結果中包含了執行腳本身啟動的進程和我們要查詢的目標進程,我們只需要過濾掉腳本本身的進程,就可以得到準確的進程ID,調整之后的腳本如下(暫時先保留 tee命令輸出的中間結果):

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函數中 grep -v $0 作用是過濾掉腳本的名字,其中 $0 表示腳本的名字 ( a.sh )

驗證

再次執行腳本,結果如下:

[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

從上面的測試結果中看出,最后輸出的結果是正確的

總結

多層管道在shell腳本中是很常見的用法,使用起來也非常方便和高效的,但是腳本一旦出問題調試就會變得困難起來,合理的使用 tee 命令輸出管道的中間結果,可以快速的定位問題所在

以上就是如何利用 tee 命令調試shell腳本中的管道的詳細內容,更多關于tee 命令調試shell腳本中的管道的資料請關注腳本之家其它相關文章!

您可能感興趣的文章:
  • Shell腳本中管道的幾種使用實例講解
  • Shell腳步攻略之管道重定向基礎
  • PowerShell管道入門必看篇(管道例子大全)
  • linux shell 管道命令(pipe)使用及與shell重定向區別
  • PowerShell實現按條件終止管道的方法
  • PowerShell中終止管道的方法
  • PowerShell入門教程之PowerShell管道介紹
  • Windows Powershell導出管道結果
  • Windows Powershell過濾管道結果
  • shell腳本一鍵同時推送代碼至github和gitee的解決辦法

標簽:河池 黔南 淮南 河北 黔南 隴南 通遼 常州

巨人網絡通訊聲明:本文標題《如何利用 tee 命令調試shell腳本中的管道》,本文關鍵詞  如何,利用,tee,命令,調試,;如發現本文內容存在版權問題,煩請提供相關信息告之我們,我們將及時溝通與處理。本站內容系統采集于網絡,涉及言論、版權與本站無關。
  • 相關文章
  • 下面列出與本文章《如何利用 tee 命令調試shell腳本中的管道》相關的同類信息!
  • 本頁收集關于如何利用 tee 命令調試shell腳本中的管道的相關信息資訊供網民參考!
  • 推薦文章
    主站蜘蛛池模板: 新宁县| 鹰潭市| 衡阳县| 鄂托克前旗| 尚义县| 滦平县| 东平县| 商河县| 理塘县| 周宁县| 宁南县| 麦盖提县| 阿拉尔市| 策勒县| 新营市| 囊谦县| 郧西县| 静宁县| 淮北市| 瑞金市| 永年县| 大化| 朝阳区| 宝清县| 荥经县| 探索| 冕宁县| 濉溪县| 沙洋县| 临朐县| 吴旗县| 灵丘县| 儋州市| 台南市| 射阳县| 和田市| 邓州市| 辉县市| 丰城市| 黔江区| 中西区|