In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-19 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >
Share
Shulou(Shulou.com)06/02 Report--
Run the script in debug mode
Through bash-x, you can run the entire script in debug mode. Bash prints out each line of commands before running, and each line is preceded by a + sign indicating the number of nesting layers of the command.
> bash-x debug.sh + echo 'First line'First line # output without plus sign + + date # first execute the command to replace the two plus signs because the command is nested in echo + echo' Print datetime: Thu 26 Mar 2020 08:21:28 PM CST Done.'Print datetime: Thu 26 Mar 2020 08:21:28 PM CST Done.
If the script is complex, we can output more detailed information by using the environment variable PS4 with the built-in variables for debugging:
> export PS4='+$ {BASH_SOURCE}: ${LINENO}: ${FUNCNAME [0]}:'> bash-x debug.sh + debug.sh:3:: echo 'First line'First line++debug.sh:4:: date+debug.sh:4:: echo' Print datetime: Thu 26 Mar 2020 08:35:59 PM CST Done.'Print datetime: Thu 26 Mar 2020 08:35:59 PM CST Done.
We can also use the DEBUG keyword of trap to execute the specified command or function before interpreting and executing each line of script:
The output of trap 'echo "VARIABLE-TRACE >\ $variable=\" $variable\ ""' DEBUGvariable=29let variable++let variable*=5exit "is as follows: VARIABLE-TRACE > $variable="VARIABLE-TRACE > $variable=" 29 "VARIABLE-TRACE > $variable=" 30 "VARIABLE-TRACE > $variable=" 150 "
You can also use the ERR keyword of trap to perform preset actions when interpreting errors, such as commands that print errors:
Trap 'echo $BASH_COMMAND' ERR
However, in view of the low efficiency of debugging with trap, it is recommended to use debugging options directly or debugging tools such as bashdb in more complex scripts.
Advanced debugging
View function call information
Using the built-in command caller in the function can output the call information of the function to stdout, but note that the command must be called inside the function.
#! / usr/bin/bashfunc1 () {for i in `seq 0 3` do echo-e "Level$i\ t `caller $i`" done} func2 () {func1} func3 () {func2} func3caller 0 # must be called in the function otherwise there is no output exit 0
Run the script to get the following output:
Direct caller of Level0 11 func2 call.sh # func1
Level1 15 func3 call.sh # layer 1 indirect call
Level2 18 main call.sh # layer 2 indirect call
Level3 # has no output because there is no layer 3 call
Local debugging
You can construct a local debug block through the set command, and we can add local debugging as follows:
Set-xdateset + x > bash script1.sh # does not need to add debug parameters The script starts now.+ dateFri 28 Feb 2020 06:23:04 PM CST+ set + xThis is a string: blackAnd this is a number: 9
Debug parameter table
Short command long command effect set-fset-o noglob disable metacharacter matching set-vset-o verbose print input command set-xset-o xtrace command line first print +, execution error will print details
Parameters for debugging can be dynamically overlaid or deleted during operation:
> set-v > datedate Fri 28 Feb 2020 06:54:47 PM CST > set-x # parameters can add up date #-v effect + date #-x effect Fri 28 Feb 2020 06:55:37 PM CST > set + vx # cancel parameter set + vx
The escape characters in the script can be significantly reduced by using the-f option:
> ls? x86_64-pc-linux-gnu-library > set-f # disable metacharacter matching > ls? ls: cannot access'?': No such file or directory > touch? > ls?'> rm? > set + f-x # option x can also be used to display detailed error messages > aaa+ aaa+'['- x / usr/lib/command-not-found']'+ / usr/lib/command-not-found-- aaaCommand 'aaa' not found Did you mean: command 'aha' from deb aha (0. 5-1) command' jaaa' from deb jaaa (0. 8-4) command 'aa' from deb astronomical-almanac (5. 6-6) Try: sudo apt install + return
Default debugging
You can also add parameters directly to the first line of the script to start the script in debug mode by default:
#! / bin/bash-xv
You can also use echo to output debugging information before commands that may go wrong:
Echo "debug message: now attempting to start w command"; w #; sort the commands to be executed by echo "Variable VARNAME is now set to $VARNAME."
Set options to assist debugging
To facilitate debugging, we can use the set command to set the options for bash:
> set-o # View the switch status of all options > set-o | grep xtracextrace off > set-x # is equivalent to set-oxtrace > set-o | grep xtrace+ grep-- color=auto xtrace+ set-oxtrace on > set + x # is equivalent to set + oxtrace + set + x > set-o | grep xtracextrace off
Common debugging options
Citation to define variables Times error:
> unset $VAR;echo $VAR > set-u # is equivalent to set-o nounset > echo $varbash: var: unbound variable
To prevent misoperation of overwriting the data in the file, set to prohibit redirection to an existing file:
> set-C # is equivalent to set-o noclobber > touch test > date > testbash: test: cannot overwrite existing file
Set not to parse wildcards:
> set-f # is equivalent to set-o noglob > touch * > ll *-rw-rw-r-- 1 remilia remilia 0 Mar 1 20:09'*'
At this point, this is the end of this article on the detailed explanation of the script debugging mechanism in bash. For more information about bash script debugging, please search for previous articles or continue to browse the relevant articles below. I hope you will support it in the future!
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.