Example
The following is a simple script. The function of the processid function in the script is to query the process ID of the specified process name. This is a very common function in the process of managing the linux server. The function of the processid function is to use multi-layer pipeline commands to query the process ID. The following is the source code of the test script
#!/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 scripts
We execute this script to query the process ID of zone9_log1. The following is the execution result
[wanng@localhost ~]$ ./ i zone9_log1 130530 144391 144392
In order to compare with the actual process ID of the zone9_log1 process, we execute the ps -ef | grep -w zone9_log1 | grep -v grep | awk '{print $2}' command separately, and the execution result is as follows:
[wanng@localhost ~]$ ps -ef | grep -w zone9_log1 | grep -v grep | awk '{print $2}' 130530
question
The same command did get different results. We added the intermediate results of the tee command output pipeline to the script. 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
Execute the script again, three files out1 out2 out3 will be generated locally, recording the intermediate results of this pipeline command. Below are the script execution results and the contents of the out1 out2 out3 file.
[wang@localhost ~]$ ./ i zone9_log1 130530 144885 144886 [wang@localhost ~]$ cat out1 wang 130530 1 0 4moon24 pts/10 00:07:47 ./zone9_log1 ./zone9_log1.lua wang 144885 109338 0 20:45 pts/8 00:00:00 /bin/sh ./ i zone9_log1 wang 144886 144885 0 20:45 pts/8 00:00:00 /bin/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 4moon24 pts/10 00:07:47 ./zone9_log1 ./zone9_log1.lua wang 144885 109338 0 20:45 pts/8 00:00:00 /bin/sh ./ i zone9_log1 wang 144886 144885 0 20:45 pts/8 00:00:00 /bin/sh ./ i zone9_log1 [wang@localhost ~]$ cat out3 130530 144885 144886 [wang@localhost ~]$
reason
When executing a script, a new shell (that is, a new process) will be created by default. The above script is executed in the new shell environment. From the above test results, we can see that the result of the ps -ef | grep -w zone9_log1 command contains the process started by the execution foot itself and the target process we want to query. We only need to filter out the process of the script itself to obtain the accurate process ID. The adjusted script is as follows (the intermediate result output by the tee command is temporarily retained):
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 above processid function grep -v $0 is used to filter out the name of the script, where $0 represents the name of the script ( )
verify
Execute the script again, and the result is as follows:
[wanng@localhost ~]$ ./ i zone9_log1 130530 [wanng@localhost ~]$ cat out1 wanng 130530 1 0 4moon24 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 4moon24 pts/10 00:07:51 ./zone9_log1 ./zone9_log1.lua [wanng@localhost ~]$ cat out3 130530
From the above test results, the final output is correct
Summarize
Multi-layer pipelines are very common in shell scripts and are very convenient and efficient to use, but once there is a problem with the script, it will become difficult to debug. Use the tee command to output the intermediate results of the pipeline correctly, so as to quickly locate the problem.
The above is the detailed content of how to use the tee command to debug pipelines in shell scripts. For more information about the tee command to debug pipelines in shell scripts, please pay attention to my other related articles!