In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-03-28 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Network Security >
Share
Shulou(Shulou.com)06/01 Report--
Debugging means of BASH
Echo/print (General Technology)
Print some variables, or prompt messages. This should be a general method. In BASH, we can simply use echo, or print to output some log, or add some loglevel to filter some log. Here is a post of my usual function:
_ loglevel=2
DIE () {
Echo "Critical: $1 >" > & 2
Exit 1
}
INFO () {
[$_ loglevel-ge 2] & & echo "INFO: $1 >" > & 2
}
ERROR () {
[$_ loglevel-ge 1] & & echo "ERROR: $1 >" > & 2
}
The implementation here simply adds a loglevel, but you can actually output the log to a file or color the log. For example:
# add color
[$_ loglevel-ge 1] & & echo-e "\ 033 [31m ERROR:\ 033 [0m $1 >" > & 2
# redirect to file
[$_ loglevel-ge 1] & & echo "ERROR: $1 >" > / var/log/xxx_log.$BASHPID
Set-x (rare technology)
The-x (xtrace) option causes BASH to print out the command to be executed before executing it. This option is useful for debugging some command errors.
Sometimes, because the parameters passed in contain some special characters, the BASH parsing is not carried out as we expected. At this point, turn on-x and you can print out the expanded command before the command is executed. For example, based on the function written earlier:
Set-x
INFO "this is an info log >"
ERROR "this is an error log >"
Set + x
If you want to open xtrace all the way, you can add the-x parameter when you execute the script.
Trap/bashdb (epic technique)
To facilitate debugging, BASH also provides a trap mechanism. This is a lot more advanced than the two methods introduced earlier. We can use the built-in command trap to specify which commands should be executed by each sigspec. Specific uses of trap are as follows:
Trap [- lp] [[arg] sigspec.]
Sigspec includes each signal, EXIT,ERR,RETURN, and DEBUG defined in.
The various signal will not be introduced here. EXIT executes the specified command when shell exits. If a command execution returns a non-zero value in the current shell, the command associated with the ERR is executed. RETURN, on the other hand, is for source and., and each execution triggers a RETURN trap. If you bind a command to DEBUG, the trap DEBUG will be executed before each command is executed. Note here that ERR and DEBUG are only valid for the current shell. If you want functions and child shell to automatically inherit these trap, you can set-T (DEBUG/RETURN) and-E (ERR).
For example, the following script executes echo when exiting:
#! / bin/bash
Trap "echo this is an exit echo >" EXIT
Echo "this is a normal echo >"
Or, let the command in the script print out when something goes wrong:
#! / bin/bash
Trap 'echo $BASH_COMMAND return err' ERR
Echo this is a normal test
UnknownCmd
Alternatively, let the script's commands step through:
#! / bin/bash
Trap'(read-p "[$0: $LINENO] $BASH_COMMAND? >") 'DEBUG
Echo this is a test
ITunes 0
While [true]
Do
Echo $I
((iTunes +))
Done
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.