Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to debug a shell script with built-in variables

2025-03-13 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/03 Report--

This article focuses on "how to debug shell scripts with built-in variables". Interested friends may wish to take a look. The method introduced in this paper is simple, fast and practical. Let the editor take you to learn how to debug shell scripts with built-in variables.

General shell script debugging is basically echo to deal with larger scripts, it is more troublesome, there is a problem, it is not very good to locate which line of code has gone wrong.

In fact, some variables built into shell can solve this problem very well:

The variables $LINENO $FUNCNAME $BASH_LINENO record the current execution location of the script, as well as the functions being executed. You can man the document page for details.

Example code: a.sh

The code is as follows:

#! / bin/bash

Abc () {

Echo "wo shi abc ()"

Echo "func: $FUNCNAME ln: $LINENO ln2:$ {BASH_LINENO [1]} brother: ${FUNCNAME [1]}"

}

B.sh:

The code is as follows:

#! / bin/bash

.. / a.sh

Abc

Cdf () {

Abc

}

Execution result:

[root@node2 ~] #. / b.sh

[code] wo shi abc ()

Func: abc ln: 5 ln2:0 brother: main

Wo shi abc () func: abc ln: 5 ln2:9 brother: cdf

I wrote the cdf function to call the abc function in b.sh. You can see the difference in the output. We can use these parameters to print out the location of the wrong line of the code and which function is called when the code goes wrong.

At this point, I believe you have a deeper understanding of "how to debug shell scripts with built-in variables". You might as well do it in practice. Here is the website, more related content can enter the relevant channels to inquire, follow us, continue to learn!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report