zoukankan      html  css  js  c++  java
  • 利用 tee 命令调试shell脚本中的管道

    在编写shell脚本时,调试是个比较麻烦的事,特别是涉及到多层管道命令的时候,会产生多个中间结果,tee命令的作用是从标准输入中读取数据写入标准输出或文件中,利用它可以从管道中读取中间结果并写入本地临时文件中,通过中间结果可以一步一步的定位到脚本的错误

    实例

    下面是一个简单的脚本,脚本中 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 命令输出管道的中间结果,可以快速的定位问题所在

  • 相关阅读:
    Oracle函数-DECODE
    WPF模板
    WPF数据编辑的提交与撤销
    WPF数据验证
    WPF多源绑定
    WPF筛选、排序和分组
    WPF绑定到集合
    输出的数据格式是如何决定的-------Asp.net WebAPI学习笔记(二)
    路由其实也可以很简单-------Asp.net WebAPI学习笔记(一)
    终结篇:RemoteWebDriver与Grid简介-----Selenium快速入门(十五)
  • 原文地址:https://www.cnblogs.com/wanng/p/pipe-debug-by-tee.html
Copyright © 2011-2022 走看看