In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-03-18 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >
Share
Shulou(Shulou.com)06/01 Report--
This article mainly introduces the example analysis of shell grammar checking mode, which is very detailed and has certain reference value. Friends who are interested must finish it!
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
Enable 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
# 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
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
# 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
The above is all the content of the article "sample Analysis of shell Grammar Checker Mode". Thank you for reading! Hope to share the content to help you, more related knowledge, welcome to follow the industry information channel!
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.