Shell Script Debugging methods

Source: Internet
Author: User

We started the Shell Script debugging series, first explaining the different debugging options, and here's how to enable Shell debug mode.

After you finish writing the script, it is recommended that you check the syntax in the script before running the script, rather than looking at their output to verify that they are working correctly.

In this part of the series, we will learn how to use the syntax to check debug mode. Remember we previously explained different debugging options in the first part of this series, where we will use them to perform script debugging.

Enable verbose debug mode

Before entering the focus of this guide, let's briefly explore the next verbose model. It can be -v enabled with the Debug option, which tells the shell to display each line as it reads.

To show how this works, here is a sample script to convert PNG images into JPG format in bulk.

Enter (or copy and paste) the following into a file.

    1. #!/bin/bash

    2. #convert

    3. for image in *.png; do

    4. convert "$image" "${image%.png}.jpg"

    5. echo "image $image converted to ${image%.png}.jpg"

    6. done

    7. exit 0

Then save the file and use the following command to make the script executable:

    1. $ chmod +x script.sh

We can execute the script and show each line it was read by the Shell:

    1. $ bash -v script.sh

Show all rows in a shell script

Enable syntax check debug mode in a Shell script

Back to the focus of our topic, -n Activate grammar check mode. It lets the shell read all the commands, but does not execute them, and it (the shell) checks the syntax only.

Once an error is found in the shell script, the shell will output an error in the terminal, otherwise it will not show anything.

The command to activate the grammar check is as follows:

    1. $ bash -n script.sh

Because the syntax in the script is correct, the command above does not show anything. So let's try to delete the end for loop to done see if the error is displayed:

The following is a modified batch of bugs containing PNG images converted to JPG format.

    1. #!/bin/bash

    2. #script with a bug

    3. #convert

    4. for image in *.png; do

    5. convert "$image" "${image%.png}.jpg"

    6. echo "image $image converted to ${image%.png}.jpg"

    7. exit 0

Save the file, then run the script and perform a grammar check:

    1. $ bash -n script.sh

Check shell script syntax

From the above output, we see an error in our script that the For loop is missing an end done keyword. The shell script checks the file from beginning to end, and once it is not found ( done ), the shell prints a syntax error:

    1. script.sh: line 11: syntax error: unexpected end of file

We can combine verbose mode and grammar check mode at the same time:

    1. $ bash -vn script.sh

Enable both verbose and grammar checking in scripts

In addition, we can enable script checking by modifying the first line of the script, as in the following example:

    1. #!/bin/bash -n

    2. #altering the first line of a script to enable syntax checking

    3. #convert

    4. for image in *.png; do

    5. convert "$image" "${image%.png}.jpg"

    6. echo "image $image converted to ${image%.png}.jpg"

    7. exit 0

Save the file and check the syntax in the run as shown above:

    1. $ ./script.sh

    2. script.sh: line 12: syntax error: unexpected end of file

In addition, we can use the built-in set command to enable debug mode in the script.

In the following example, we only examine the FOR loop syntax in the script.

    1. #!/bin/bash

    2. #using set shell built-in command to enable debugging

    3. #convert

    4. #enable debugging

    5. set -n

    6. for image in *.png; do

    7. convert "$image" "${image%.png}.jpg"

    8. echo "image $image converted to ${image%.png}.jpg"

    9. #disable debugging

    10. set +n

    11. exit 0

Save and execute the script again:

    1. $ ./script.sh

In general, we should ensure that the script syntax is checked before executing the Shell script to catch errors.

Please send us any questions or feedback about this guide in the feedback section below. In the third part of this series, we will explain and use the shell to track debug mode.

Author Profile:

Aaron Kili is a Linux and f.o.s.s enthusiast who is about to be a Linux system administrator, Web Developer, and currently a tecmint content creator who likes to work with computers and share knowledge.

Aaron Kili[1] Translator: GEEKPI proofreading: Jasminepeng

This article by Lctt[2] original compilation, Linux China honor launched

Shell Script Debugging methods

Related Article

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.