In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-28 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >
Share
Shulou(Shulou.com)06/01 Report--
This article will explain in detail how to implement syntax checking debugging mode in Shell scripts. The editor thinks it is very practical, so I share it with you as a reference. I hope you can get something after reading this article.
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 # convert for image in * .png; do convert "$image"${image%.png} .jpg" echo "image $image converted to ${image%.png} .jpg" done exit 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
Show all lines in the shell script
Enable syntax checking debug mode in Shell scripts
Going back to the focus of our topic,-n activates the syntax checking mode. 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 # convert for image in * .png; do convert "$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
Check the shell script syntax
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
Enable both verbose checking and syntax checking in the 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 # convert for image in * .png; do convert "$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.sh 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.
#! / bin/bash # using set shell built-in command to enable debugging # convert # enable debugging set-n for image in * .png; do convert "$image"${image%.png} .jpg" echo "image $image converted to ${image%.png} .jpg" # disable debugging set + n exit 0
Save and execute the script again:
$. / script.sh on "how to perform syntax checking debugging mode in Shell scripts" this article is shared here, I hope the above content can be of some help to you, so that you can learn more knowledge, if you think the article is good, please share it for more people to see.
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.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.