How to use tee command to debug pipeline in shell script

Time:2021-7-27

example

The following is a simple script. The ProcessId function in the script is used to query the process ID of the specified process name. This is a very common function in the process of managing Linux server. The ProcessId function is used to query the process ID by using multi-layer pipeline commands. The following is the test script source code


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

Execute script

We execute this script to query zone9_ The process ID of Log1. The following is the execution result


[[email protected] ~]$ ./a.sh i zone9_log1
130530 144391 144392

For and zone9_ Compared with the actual process ID of the Log1 process, we execute PS – EF | grep – w zone9 separately_ Log1 | grep – V grep | awk ‘{print $2}’ command, the execution result is as follows:


[[email protected] ~]$ ps -ef | grep -w zone9_log1 | grep -v grep | awk '{print $2}'
130530

problem

The same command does get different results. We add the tee command to the script to output the intermediate results of the pipeline. The adjusted script is as follows:


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

When the script is executed again, three files of out1 out2 out3 will be generated locally to record the intermediate results of the pipeline command. The following are the script execution results and the contents of the out1 out2 out3 file

[[email protected] ~]$ ./a.sh i zone9_log1
130530 144885 144886

[[email protected] ~]$ cat out1
Wang 130530 April 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
[[email protected] ~]$ cat out2
Wang 130530 April 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
[[email protected] ~]$ cat out3
130530
144885
144886
[[email protected] ~]$

reason

When executing the script, a new shell (that is, a new process) will be created by default. The above script a.sh is executed in the new shell environment. As can be seen from the above test results, PS – EF | grep – w zone9_ The result of the Log1 command includes the process started by the execution foot itself and the target process we want to query. We only need to filter the process of the script itself to get the accurate process ID. the adjusted script is as follows (the intermediate result output by the tee command is reserved for the time being):


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

In the ProcessId function above, grep – V $0 is used to filter out the name of the script, where $0 represents the name of the script (a.sh)

verification

Execute the script again and the results are as follows:

[[email protected] ~]$ ./a.sh i zone9_log1
130530

[[email protected] ~]$ cat out1
Wang 130530 April 24 PTS / 10 00:07:51. / zone 9_ log1 ./zone9_ log1.lua
wanng       146170 146168  0 21:11 pts/8    00:00:00 grep -w zone9_log1
[[email protected] ~]$ cat out2
Wang 130530 April 24 PTS / 10 00:07:51. / zone 9_ log1 ./zone9_ log1.lua
[[email protected] ~]$ cat out3
130530

From the above test results, it can be seen that the final output result is correct

summary

Multi layer pipeline is a common usage in shell script, and it is also very convenient and efficient to use. However, once the script has a problem, debugging will become difficult. Using TEE command reasonably to output the intermediate results of the pipeline can quickly locate the problem

The above is the details of how to use tee command to debug the pipeline in shell script. For more information about tee command to debug the pipeline in shell script, please pay attention to other related articles of developeppaer!