Linux shell script series tutorial (VII): script debugging

Time:2022-1-18

1、 Debug script

Debugging function is one of the important features that every programming language should realize. When there are some unexpected situations, it is used to generate script operation information. Debugging information can help you find out what causes the program to crash or abnormal behavior.
2、 Bash built-in debugging function

Using bash’s built-in debugging function, you can debug the entire script or only some statements in the script.
#Use set – X and set + X to debug a sentence of the script

Copy codeThe code is as follows:
#!/bin/bash
for i in { 1 2 3 4 5 6 };
do
Set – x # enable debugging function
Echo $I # statement to debug
Set + X # turns off the debugging function
done
echo “Script executed.”

Copy codeThe code is as follows:
#Use the – x option to debug the entire script
bash -x script. SH # is equivalent to SH – x script sh

3、 Custom debugging function

Bash’s built-in debugging function can only output debugging information in a fixed format, but in many cases, we need to display debugging information in a custom format, which can be_ Debug environment variable to establish this kind of debugging style.

Copy codeThe code is as follows:
#Use_ DEBUG=on bash script. SH run the following script
#!/bin/bash
function DEBUG()
{
[“$_debug” = = “on”] & [email protected] |: # use_ Debug environment variable debugging
}
for i in { 1 2 3 }
do
    DEBUG echo $i
done     

We debug in front of every statement that needs to print debugging information. If we don’t_ Debug = on pass a script, then the debugging information will not be printed. In Bash,: tell the shell not to do anything.

Recommended Today

Big data Hadoop — spark SQL + spark streaming

catalogue 1、 Spark SQL overview 2、 Sparksql version 1) Evolution of sparksql 2) Comparison between shark and sparksql 3)SparkSession 3、 RDD, dataframes and dataset 1) Relationship between the three 1)RDD 1. Core concept 2. RDD simple operation 3、RDD API 1)Transformation 2)Action 4. Actual operation 2)DataFrames 1. DSL style syntax operation 1) Dataframe creation 2. SQL […]