In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-31 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >
Share
Shulou(Shulou.com)06/01 Report--
This article mainly introduces how to track the execution of debugging commands in the Shell script, which is very detailed and has a certain reference value. Friends who are interested must read it!
The previous part of this series clearly clarified the other two shell script debugging modes: verbose mode and syntax checking mode, and showed how to enable shell script debugging in these modes with easy-to-understand examples.
How to enable debug mode for Shell scripts in Linux
How to perform syntax checking debug mode in a Shell script
Shell tracing is simply about tracking the execution of commands in shell scripts. To turn on shell tracing, use the-x debugging option.
This causes shell to display all executed commands and their parameters on the terminal.
We will use the following sys_info.sh shell script, which will briefly print your system date and time, the number of users logged in, and the running time of the system. However, the script contains syntax errors that we need to find and correct.
#! / bin/bash # script to print brief system info ROOT_ID= "0" DATE= `date`NO_USERS= `who | wc-l`date`uptime` check_root () {if ["$UID"-ne "$ROOT_ID"]; then echo "You are not allowed to execute this program!" Exit 1;} print_sys_info () {echo "System Time: $DATE" echo "Number of users: $NO_USERS" echo "System Uptime: $UPTIME} check_root print_sys_info exit 0
Save the file and execute the script. The script can only be run with the root user, so run with the sudo command as follows:
$chmod + x sys_info.sh $sudo bash-x sys_info.sh
Shell trace-displays errors in the script
From the output above, we can observe that the command is executed first, and then the output is taken as the value of a variable.
For example, date is executed first, and its output is the value of the variable DATE.
We can perform a syntax check to show only the syntax errors, as shown below:
$sudo bash-n sys_info.sh
Syntax check in script
If we look at the shell script, we will see that the if statement lacks the fi keyword for the enclosing condition. So, let's add it, and the new script should look like this:
#! / bin/bash # script to print brief system info ROOT_ID= "0" DATE= `date`NO_USERS= `who | wc-l`date`uptime` check_root () {if ["$UID"-ne "$ROOT_ID"]; then echo "You are not allowed to execute this program!" Exit 1; fi} print_sys_info () {echo "System Time: $DATE" echo "Number of users: $NO_USERS" echo "System Uptime: $UPTIME} check_root print_sys_info exit 0
Save the file again and execute it in root, while doing a syntax check:
$sudo bash-n sys_info.sh
Perform syntax checking in shell scripts
The result of the above syntax check operation still shows an error on line 21 of the script. Therefore, we still have to correct some grammar.
If you analyze the script again, you will find that the error on line 21 is due to the fact that there are no closed double quotes in an echo command in the print_sys_info function.
We will add closed double quotes to the echo command and save the file. The modified script is as follows:
#! / bin/bash # script to print brief system info ROOT_ID= "0" DATE= `date`NO_USERS= `who | wc-l`date`uptime` check_root () {if ["$UID"-ne "$ROOT_ID"]; then echo "You are not allowed to execute this program!" Exit 1; fi} print_sys_info () {echo "System Time: $DATE" echo "Number of users: $NO_USERS" echo "System Uptime: $UPTIME"} check_root print_sys_info exit 0
Now check the grammar again.
$sudo bash-n sys_info.sh
The above command does not produce any output because our script is syntactically correct. We can also track script execution again, which should work well:
$sudo bash-x sys_info.sh
Track shell script execution
Now run the script.
$sudo. / sys_info.sh
Display date, time, and run time with shell script
The importance of shell tracking execution
Shell script tracking can help us identify syntax errors and, more importantly, logic errors. For example, the check_root function in the sys_info.sh shell script is used to determine whether the user is root, because the script is only allowed to be executed by the superuser.
Check_root () {if ["$UID"-ne "$ROOT_ID"]; then echo "You are not allowed to execute this program!" Exit 1; fi}
The magic here is controlled by the if statement expression ["$UID"-ne "$ROOT_ID"], and once we don't use the appropriate numeric operator (- ne in the example, which means inequality), we may end up with a logic error.
Suppose we use-eq (which means equal to), which will allow any system user as well as root users to run scripts, so it is a logic error.
Check_root () {if ["$UID"-eq "$ROOT_ID"]; then echo "You are not allowed to execute this program!" Exit 1; fi}
Note: as we described at the beginning of this series, set, the shell built-in command, activates debugging in specific parts of the shell script.
Therefore, the following line will help us find this logic error in it by tracking the execution of the script:
Scripts with logic errors:
#! / bin/bash # script to print brief system info ROOT_ID= "0" DATE= `date`NO_USERS= `who | wc-l`date`uptime` check_root () {if ["$UID"-eq "$ROOT_ID"]; then echo "You are not allowed to execute this program!" Exit 1; fi} print_sys_info () {echo "System Time: $DATE" echo "Number of users: $NO_USERS" echo "System Uptime: $UPTIME"} # turning on and off debugging of check_root function set-x; check_root; set + x; print_sys_info exit 0
Save the file and call the script, and in the output, we can see that a normal system user can run the script without sudo. This is because the value of USER_ID is 100, which is not equal to the ROOT_ID of root with 0.
$. / sys_info.sh
The above is all the contents of the article "how to track the execution of debugging commands in Shell scripts". 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.