Get the App
SLTechnology News&Howtos  ›  Development  › 

How to implement Linux Shell script debugging

Shulou Source: shulou.com Published: 2022-06-03 09:25:41 10月22日 Update

This article mainly explains "how to achieve Linux Shell script debugging", the content of the article is simple and clear, easy to learn and understand, the following please follow the editor's ideas slowly in depth, together to study and learn "how to achieve Linux Shell script debugging" bar!

Debug script

Debugging function is one of the important features that every programming language should implement. When something unexpected happens, use it to generate the running information of the script. Debugging information can help you figure out what causes the program to crash or behave abnormally.

II. Built-in debugging function of Bash

Using the built-in debugging feature of Bash, 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

The code is as follows:

#! / bin/bash

For i in {1 2 3 4 5 6}

Do

Set-x # enables debugging

Echo $I # statements to debug

Set + x # turns off debugging

Done

Echo "Script executed."

The code is as follows:

# debug the entire script using the-x option

Bash-x script.sh # is equivalent to sh-x script.sh

III. Custom debugging functions

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 established through the _ DEBUG environment variable.

The code is as follows:

# run the following script using _ DEBUG=on bash script.sh

#! / bin/bash

Function DEBUG ()

{

["$_ DEBUG" = = "on"] & & $@ | |: # debug using the _ DEBUG environment variable

}

For i in {1 2 3}

Do

DEBUG echo $I

Done

We DEBUG in front of every statement that needs to print debug information, and if we don't pass a script to _ DEBUG=on, the debug information won't be printed. In Bash,: tell shell not to do anything.

Thank you for your reading, the above is the content of "how to achieve Linux Shell script debugging", after the study of this article, I believe you have a deeper understanding of how to achieve Linux Shell script debugging, and the specific use needs to be verified in practice. Here is, the editor will push for you more related knowledge points of the article, welcome to follow!

Tags: Script function information code situation statement learning content variable format environment run important unexpected reason can be passed starting material that is ideas articles Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno vpn Xiaomi Apple MySQL OPPO Reno