What are the built-in debugging methods for Bash scripts
This article introduces the relevant knowledge of "what are the built-in debugging methods for Bash scripts". In the operation of actual cases, many people will encounter such a dilemma, so let the editor lead you to learn how to deal with these situations. I hope you can read it carefully and be able to achieve something!
Scripts written in Bash can also be debugged, just like interpretive languages such as Python,Perl. Create a new script named servinfo and add executable permissions:
The code is as follows:
$vi servinfo
#! / bin/bash
Echo "Hostname: $(hostname)"
Echo "Date: $(date)"
Echo "Kernel: $(uname-mrs)"
$chmod + x servinfo
To debug the above script with bash-x, Bash first prints out each line of script, and then prints out the execution result of each line of script:
The code is as follows:
$bash-x servinfo
+ + hostname
+ echo 'Hostname: vpsee'
Hostname: vpsee
+ + date
+ echo 'Date: Thu Sep 3 19:33:48 SAST 2009'
Date: Thu Sep 3 19:33:48 SAST 2009
+ + uname-mrs
+ echo 'Kernel: Linux 2.6.18-128.4.1.el5 i686'
Kernel: Linux 2.6.18-128.4.1.el5 i686
If you want to print the line number at the same time, you can add:
The code is as follows:
Export PS4='+$ {BASH_SOURCE}: ${LINENO}: ${FUNCNAME [0]}:'
The execution result is:
The code is as follows:
$bash-x servinfo
+ export 'PS4=+$ {BASH_SOURCE}: ${LINENO}: ${FUNCNAME [0]}:'
+ PS4='+$ {BASH_SOURCE}: ${LINENO}: ${FUNCNAME [0]}:'
+ + 4VRV 5VRV: hostname
+ 4 echo 5 Hostname: vpsee'
Hostname: vpsee
+ + 4VRV 6vl: date
+ 4 SAST 6: echo 'Date: Thu Sep 3 19:42:06 SAST 2009'
Date: Thu Sep 3 19:42:06 SAST 2009
+ + 4VRV 7vl: uname-mrs
+ 4 128.4.1.el5 7: echo 'Kernel: Linux 2.6.18-128.4.1.el5 i686'
Kernel: Linux 2.6.18-128.4.1.el5 i686
If you only want to debug a few lines of scripts, you can use set-x and set + x to include the parts you want to debug:
The code is as follows:
#! / bin/bash
Echo "Hostname: $(hostname)"
Set-x
Echo "Date: $(date)"
Set + x
Echo "Kernel: $(uname-mrs)"
At this point, you can run the script directly, without the need to execute bash-x:
The code is as follows:
$. / servinfo
Hostname: vpsee
+ + date
+ echo 'Date: Thu Sep 3 19:46:53 SAST 2009'
Date: Thu Sep 3 19:46:53 SAST 2009
+ set + x
Kernel: Linux 2.6.18-128.4.1.el5 i686
If you want to debug a very complex Bash script, it is recommended to use a special debugging tool, such as bashdb
This is the end of the content of "what are the built-in debugging methods for Bash scripts". Thank you for reading. If you want to know more about the industry, you can follow the website, the editor will output more high-quality practical articles for you!