How to perform a syntax check in a Shell script debug mode

Source: Internet
Author: User
Tags webp

Label: How to Perform Syntax Check Debug Mode in a Shell Script

We have opened a series of articles on debugging shell scripts, which first explained the different debugging options. The following describes how to enable shell debugging mode.

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

In this part of the series, we will learn how to check debug mode using syntax. Keep in mind that we explained different debugging options in the first part of this series, here we will use them to perform script debugging.

Enable verbose debug mode

Before getting into the focus of this guide, let's briefly explore verbose mode. It can be enabled with the -v debug option, which tells the shell to display each line as it is read.

To show how this works, here is a sample script to batch replace images.

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

#! / bin / bash # convertfor image in * .png; doconvert "$ image" "$ {image% .png} .jpg" echo "image $ image converted to $ {image% .png} .jpg" doneexit 0
Then save the file and make the script executable with the following command:

$ chmod + x script.sh
We can execute the script and display every line it reads by the shell:

$ bash -v script.sh
650) this.width = 650; "title =" Show all the lines in the shell script "src =" http://mmbiz.qpic.cn/mmbiz_png/W9DqKgFsc693NKU03SF1EtPAuUyQlzTiaFjd0C3ic7woUSG2&4&?wwxww&xwwww&xwwwww+www+wwwwwwww+wwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwww = "margin: 0px; padding: 0px; height: auto; border: none; width: auto;" alt = "640? wx_fmt = png & tp = webp & wxfrom = 5 & wx_lazy =" />

Show all lines in shell script

Enable Syntax Checking Debug Mode in Shell Script

Back to the main point of our topic, -n activates the syntax check mode. It will make the shell read all the commands, but not execute them, it will only check the syntax.

Once an error is found in the shell script, the shell will output the error in the terminal, otherwise nothing will be displayed.

The command to activate the syntax check is as follows:

$ bash -n script.sh
Because the syntax in the script is correct, the above command will not display anything. So let's try to delete the done that ends the for loop to see if an error is displayed:

The following is a modified script that converts png images to jpg format in a batch with bugs.

#! / bin / bash # script with a bug # convertfor image in * .png; doconvert "$ image" "$ {image% .png} .jpg" echo "image $ image converted to $ {image% .png}. jpg "exit 0
Save the file, then run the script and perform a syntax check:

$ bash -n script.sh
650) this.width = 650; "title =" Check Shell Script Syntax "src =" http://mmbiz.qpic.cn/mmbiz_png/W9DqKgFsc693NKU03SF1EtPAuUyQlzTiauDSgC5lpmQAhwNe0EicddG=Mwxx618q48ZsJPEtiauCy_wwp_mw_tww_twwp_wwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwww.ww : 0px; padding: 0px; height: auto; border: none; width: auto; "alt =" 640? Wx_fmt = png & tp = webp & wxfrom = 5 & wx_lazy = "/>

Check shell script syntax

From the output above, we see an error in our script that the for loop is missing an ending done keyword. The shell script checks the file from beginning to end, and once it doesn't find it, the shell prints a syntax error:

script.sh: line 11: syntax error: unexpected end of file
We can combine both verbose mode and syntax check mode:

$ bash -vn script.sh
650) this.width = 650; "title =" Enable both verbose checking and syntax checking in the script "src =" http://mmbiz.qpic.cn/mmbiz_png/W9DqKgFsc693NKU03SF1EtPAuUyQlzTia0FraozjjDFFTRW&Wxxww&www=wwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwww 1 "style =" margin: 0px; padding: 0px; height: auto; border: none; width: auto; "alt =" 640? Wx_fmt = png & tp = webp & wxfrom = 5 & wx_lazy = "/>

Enable both verbose and syntax checking in a script

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

#! / bin / bash -n # altering the first line of a script to enable syntax checking # convertfor image in * .png; doconvert "$ image" "$ {image% .png} .jpg" echo "image $ image converted to $ {image% .png} .jpg "exit 0
As shown above, save the file and check the syntax on the fly:

$ ./script.shscript.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 check the for loop syntax in the script.

#! / bin / bash # using set shell built-in command to enable debugging # convert # enable debuggingset -nfor image in * .png; doconvert "$ image" "$ {image% .png} .jpg" echo "image $ image converted to $ {image% .png} .jpg "#disable debuggingset + nexit 0
Save and execute the script again:

$ ./script.sh
In general, we should ensure that the script syntax is checked to catch errors before executing the shell script.



This article is from the "LINUX Super Dream" blog, please be sure to keep this source http://215687833.blog.51cto.com/6724358/1891627

How to perform syntax check debug mode in a shell script

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.