Shell debugging

  
 

This article comprehensively and systematically introduces shell script debugging techniques, including outputting key information using echo, tee, trap, etc., tracking the value of variables, embedding debug hooks in scripts, and using the “-n” option for shell scripts Grammar check, use the "-x" option to achieve shell script tracking one by one, clever use of the shell's built-in variables to enhance the output information of the "-x" option. I. Preface

Shell programming is widely used in the unix/linux world. Mastering shell programming is also a must for an excellent Unix/linux developer and system administrator. The main job of script debugging is to find out the cause of the script error and locate the line where the error occurred in the script source code. Commonly used methods include analyzing the output error information, and by adding debugging statements to the script, output debugging information to assist in the diagnosis of errors. Use debugging tools, etc. However, compared with other high-level languages, the shell interpreter lacks the corresponding debugging mechanism and debugging tools. The output error information is often unclear. When the beginner is debugging the script, in addition to knowing to output some information with the echo statement, There is no other way, but relying on a large number of echo statements to diagnose errors is really incomprehensible, so common beginners complain that shell scripts are too difficult to debug. This article will systematically introduce some important shell script debugging techniques, hoping to benefit the beginners of the shell.

The target audience of this article is the developers, testers and system administrators in the Unix/Linux environment, which requires the reader to have basic knowledge of shell programming. The examples used in this article were tested under Bash 3.1 + Redhat Enterprise Server 4.0, but the debugging techniques should be equally applicable to other shells.

Second. Output debugging information in shell scripts

It is the most common debugging method to display relevant information of some key places or errors by adding debugging statements in the program. Shell programmers usually use echo (ksh programmers often use print) statements to output information, but relying solely on the output of the echo statement to trace information is cumbersome. The large number of echo statements added to the script during the debugging phase have to be laborious at the time of product delivery. One delete. In response to this problem, this section mainly introduces some methods for how to output debugging information conveniently and efficiently.

1. Using the trap command The

trap command is used to capture the specified signal and execute a predefined command. The basic syntax is: trap 'command' signal where signal is the signal to be captured, and command is the command to be executed after the specified signal is captured. You can use the kill –l command to see all the available signal names in the system. The commands executed after the signal is captured can be any one or more legal shell statements, or a function name. When the shell script is executed, it will generate three so-called "pseudo-signals", which are called "false signals" because the three signals are generated by the shell, and the other signals are caused by < u> operating system
generated), by using the trap command to capture these three "false signals" and output related information is very helpful for debugging.


Table 1. When the shell pseudo-signal signal name is generated EXIT Exits from a function or the entire script is executed ERR When a command returns a non-zero state (representing the command execution is unsuccessful) Before each command in the DEBUG script is executed

By capturing the EXIT signal, we can output the value of some variable that we want to track when the shell script aborts or exits from the function, and thus judges the script. The execution status and the cause of the error are: trap 'command' EXIT or trap 'command' 0

By capturing the ERR signal, we can easily trace the unsuccessful command or function and output the relevant The debug information, the following is a sample program that captures the ERR signal, where $LINENO is a shell built-in variable that represents the current line number of the shell script.

$ cat -n exp1.sh 1 ERRTRAP() 2 { 3 echo "[LINE:$1] Error: Command or function exited with status $?" 4 } 5 foo() 6 { 7 return 1; 8 } 9 trap 'ERRTRAP $LINENO' ERR 10 abc 11 foo

The output is as follows:

$ sh exp1.sh exp1.sh: line 10: abc: command not found [LINE:10] Error: Command or function exited with status 127 [LINE:11] Error: Command or function exited with status 1

In the debugging process, in order to track the value of certain variables, we often need Inserting the same echo statement in many places in a shell script to print the value of a related variable is cumbersome and clumsy. By capturing the DEBUG signal, we only need a trap statement to complete the tracking of the relevant variables.

The following is an example program to track variables by capturing DEBUG signals:

$ cat –n exp2.sh 1 #!/bin/bash 2 trap 'echo “before execute line :$LINENO, a=$a,b=$b,c=$c”' DEBUG 3 a=1 4 if [ "$a" -eq 1 ] 5 then 6 b=2 7 else 8 b=1 9 Fi 10 c=3 11 echo "end"

The output is as follows:

$ sh exp2.sh before execute line:3, a=,b=,c= before execute line :4, a=1,b=,c= before execute line:6, a=1,b=,c= before execute line:10, a=1,b=2,c= before execute line:11, a =1, b=2, c=3 end

From the running results, it is clear that the value of the relevant variable changes after each command is executed. At the same time, from the line number printed out in the running result, you can see the execution trajectory of the entire script, and you can determine which conditional branches are executed and which conditional branches are not executed.

2. Using the tee command

In the shell script, the pipeline and input and output redirection are used very much. Under the action of the pipeline, the execution result of some commands directly becomes the next command. Input. If we find that the execution results of a batch of commands connected by pipes are not as expected, we need to step through the execution results of each command to determine where the problem is, but because of the use of pipes, these intermediate results are not displayed. On the screen, it is difficult to debug, and we can use the tee command.

Copyright © Windows knowledge All Rights Reserved