shell脚本_利用 tee 命令调试shell脚本

0c2eefb898d095fdc01c996275140515.png

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

实例

下面是一个简单的脚本,脚本中 processid 函数的作用是查询指定进程名字的进程ID,在管理linux服务器的过程中,这个是很常见的功能,processid 函数作用是利用多层管道命令查询进程ID,以下是测试脚本源码

#!/bin/shprocessid(){    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

执行脚本

我们执行这个脚本查询 game9_log1 的进程ID,下面是执行的结果

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

为了和 game_log1 进程实际的进程ID对比,我们单独执行 ps -ef | grep -w game9_log1 | grep -v grep | awk '{print $2}' 命令,执行结果如下:

[wanng@localhost ~]$ ps -ef | grep -w game9_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 game9_log1130530 144885 144886[wang@localhost ~]$ cat out1wang      130530      1  0 4月24 pts/10  00:07:47 ./game9_log1 ./game9_log1.luawang       144885 109338  0 20:45 pts/8    00:00:00 /bin/sh ./a.sh i game9_log1wang       144886 144885  0 20:45 pts/8    00:00:00 /bin/sh ./a.sh i game9_log1wang       144888 144886  0 20:45 pts/8    00:00:00 grep -w game9_log1[wang@localhost ~]$ cat out2wang      130530      1  0 4月24 pts/10  00:07:47 ./game9_log1 ./game9_log1.luawang       144885 109338  0 20:45 pts/8    00:00:00 /bin/sh ./a.sh i game9_log1wang       144886 144885  0 20:45 pts/8    00:00:00 /bin/sh ./a.sh i game9_log1[wang@localhost ~]$ cat out3130530144885144886[wang@localhost ~]$

原因

执行脚本的时候,默认会创建一个新的shell(也即一个新的进程),上面的脚本 a.sh 就是在新的shell环境中执行的。从上面的测试结果可以看出,ps -ef | grep -w game9_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 game9_log1130530[wanng@localhost ~]$ cat out1wanng      130530      1  0 4月24 pts/10  00:07:51 ./game9_log1 ./game9_log1.luawanng       146170 146168  0 21:11 pts/8    00:00:00 grep -w game9_log1[wanng@localhost ~]$ cat out2wanng      130530      1  0 4月24 pts/10  00:07:51 ./game9_log1 ./game9_log1.lua[wanng@localhost ~]$ cat out3130530

从上面的测试结果中看出,最后输出的结果是正确的

总结

多层管道在shell脚本中是很常见的用法,使用起来也非常方便和高效的,但是脚本一旦出问题调试就会变得困难起来,合理的使用 tee 命令输出管道的中间结果,可以快速的定位问题所在

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值