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 check shell syntax

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

Share

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

This article mainly introduces the relevant knowledge of "how to check shell Grammar". The editor shows you the operation process through actual cases. The operation method is simple, fast and practical. I hope this article "how to check shell Grammar" can help you solve the problem.

Enable verbose debug mode

Before moving on to the focus of this guide, let's briefly explore the verbose pattern. It can be enabled with the-v debug option, which tells shell to display each line when reading. To show how this works, here is an example script that batch converts PNG images to JPG format.

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 each line it reads by Shell:

$bash-v script.sh enables syntax checking debug mode in Shell scripts

Activate syntax checking mode using-n

It makes shell read all commands, but does not execute them, it (shell) only checks syntax. Once an error is found in the shell script, 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 does not show 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 batch script with bug to convert png images into jpg format.

#! / 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

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

Script.sh: line 11: syntax error: unexpected end of file

We can combine both verbose mode and syntax checking mode:

$bash-vn script.sh

We can also enable script checking by modifying the first line of the script

Such as 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 at run:

$. / 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 examine 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 make sure that we check the script syntax to catch errors before executing the Shell script.

This is the end of the introduction to "how to check shell Grammar". Thank you for reading. If you want to know more about the industry, you can follow the industry information channel. The editor will update different knowledge points for you every day.

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