How to enable the debugging mode of Shell scripts in Linux

Source: Internet
Author: User
Tags call shell

How to enable the debugging mode of Shell scripts in Linux
GuideScripts are a series of commands stored in a file. Input commands on the terminal. The method of sequential execution is too weak. using scripts, users in the system can store all commands in one file, the file is repeatedly called to re-execute the command multiple times.

In the early stages of learning scripts or writing scripts, we usually start from short scripts that write small scripts or several lines of commands, when debugging such a script, we usually just observe their output to ensure its normal operation. However, when we begin to write Advanced scripts with very long or thousands of lines of commands, such as scripts that change system settings, and execute critical backups on the network, we will realize that the Script output alone is not enough to find bugs in the script! Therefore, the introduction of Shell script debugging in the Linux series will show you how to enable Shell script debugging, and then explain different Shell script debugging modes and how to use them in subsequent series.

How to start writing a script

The difference between a script and other files is its first line, which contains #! (She-Bang-release companion: defines the file type) and path name (Interpreter path), notifies the system that the file is a command set and will be interpreted by the specified program (Interpreter.
The following is an example of the first line 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 one group of standard system commands, there are no internal Shell commands, the first line or #! Can be removed.

How to execute Shell scripts in Linux

The general syntax for calling a script is:

$ Script name parameter 1... parameter N

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

$ Shell script name parameter 1... parameter N

Example:

$/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, the script only contains the basic system commands, for example:

### The script only contains the standard system command cd/home/$ USERmkdir tmpecho "tmp directory created under/home/$ USER"

Make it executable and run as follows:

$ Chmod + x Script Name $./Script Name
How to enable the Shell script debugging mode

The following are the main Shell script debugging options:

-V (verbose for short)-tells the Shell to display all rows when reading the script and activate the verbose mode. -N (noexec or no ecxecution)-indicates that the Shell reads all commands but does not execute them. This option activates the syntax check mode. -X (xtrace or execution trace for short)-tells Shell to display all executed commands and their parameters on the terminal. This option enables Shell tracing mode.
1. Change the first line of Shell script

The first mechanism is to change the first line of the Shell script, as shown below, which will start script debugging.

#! /Bin/sh Option

The options can be one or more debugging options mentioned above.

2. Call Shell debugging options

The second is to use the following debugging options to start the Shell. This method will also enable the entire script debugging.

$ Shell option parameter 1... parameter N

Example:

$/Bin/bash option parameter 1... parameter N
3. Use the Shell built-in command set

The third method is to use the built-in command set to debug a given Shell script, such as a function. This mechanism is important because it allows us to debug any Shell script.
We can use the set command to open the debugging mode as follows. The options are all the debugging options mentioned earlier.

$ Set Option

Enable debug mode:

$ Set-Option

Disable debug mode:

$ Set + Option

In addition, if we enable several debugging modes in different parts of the Shell script, we can disable all debugging modes at one time, as shown below:

$ set -

Let's talk about how to enable the Shell script debugging mode first. As we can see, We can debug an entire Shell script or a specific part of the script.
In the two articles in this series, we will give an example of how to use the Shell script debugging option to learn more about verbose, syntax checking, and tracing) debugging mode.
More importantly, I hope this guide will help you.

From: https://linux.cn: 443/article-8028-1.html

Address: http://www.linuxprobe.com/how-linuxshell-script.html


Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.