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 enable debug mode of Shell script in Linux

2025-02-24 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >

Share

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

This article mainly explains "how to enable Shell script debugging mode in Linux", interested friends may wish to take a look. The method introduced in this paper is simple, fast and practical. Let's take you to learn "How to enable Shell script debugging mode in Linux"!

A script is a series of commands stored in a file. The method of inputting commands one by one on the terminal and executing them sequentially is too weak. With scripts, users in the system can store all commands in a file and repeatedly call the file to execute the commands again and again.

In the early stages of learning or writing scripts, we usually start by writing small scripts or short scripts with a few lines of commands. When debugging such scripts, we usually do nothing more than observe their output to make sure they work properly.

However, when we start writing advanced scripts that are very long or thousands of lines long, such as scripts that change system settings, perform critical backups on the network, etc., we realize that just looking at the script output is not enough to find bugs in the script!

So, in this article on Shell scripting in the Linux series, we'll look at how to enable Shell scripting, and then explain the different Shell scripting modes and how to use them in later series.

How to Start Writing a Script

A script is distinguished from other files by its first line, which contains #! (She-Bang -Interpreter: defines the file type) and pathname (interpreter path), informing the system that the file is a collection of commands to be interpreted by the specified program (interpreter).

Here are examples of the first lines of different types of scripts:

#!/ bin/sh [sh Script] #!/ bin/bash [bash Script] #!/ usr/bin/perl [perl Program] #!/ bin/awk -f [awk script]

Note: If the script contains only a set of standard system commands, without any internal Shell directives, the first line or #! can be removed.

How to execute Shell scripts on Linux

The general syntax for invoking a script is:

$Script Name Parameters 1... parameter N

Another possible form is to explicitly specify the Shell that will execute the script, as follows:

$ Shell Script Name Parameters 1... parameter N

Examples:

$ /bin/bash Parameter 1... parameter N [bash Script] $ /bin/ksh Parameter 1... parameter N [ksh Script] $ /bin/sh Parameter 1... parameter N [sh script]

For no #! As the first line, a script containing only the base system commands, an example of which is as follows:

###Script contains only standard system commands cd /home/$USER mkdir tmp echo "tmp directory created under /home/$USER"

Make it executable and operational as follows:

$ chmod +x script name $ ./ script name

How to Enable Shell Script Debugging Mode

Here are the main Shell script debugging options:

-v (short for verbose) -Tells Shell to display all lines when reading script, activating verbose mode.

-n (noexec or no execution for short) -instructs the Shell to read all commands but not execute them. This option activates syntax checking mode.

-x (xtrace or execution trace for short) -tells the Shell to display all executed commands and their arguments on the terminal. This option enables Shell Trace mode.

1. Change the first line of Shell script

*** One mechanism is to change the first line of the Shell script, as follows, which will initiate script debugging.

#!/ bin/sh option

Among other things, the options may be one or more of the debug options mentioned above.

2. Call Shell debugging options

The second is to start the Shell with the following debugging option, which also turns on debugging of the entire script.

$ Shell Options Parameters 1... parameter N

Examples:

$ /bin/bash Options Parameters 1... parameter N

3. Use Shell built-in command set

The third method is to use the built-in command set to debug a given Shell script part, such as a function. This mechanism is important because it allows us to debug any Shell script.

We can turn on debug mode using the set command as follows, where the options are all of the debug options mentioned earlier.

$ set option

Enable debug mode:

$ set -Options

Disable debug mode:

$ set + option

Also, if we enable several debug modes in different parts of the Shell script, we can disable all debug modes at once, as follows:

$ set -At this point, I believe that everyone has a deeper understanding of "how to enable Shell script debugging mode in Linux", so let's actually operate it! Here is the website, more related content can enter the relevant channels for inquiry, pay attention to 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

Servers

Wechat

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

12
Report