在Shell脚本中,您可以使用-x调试选项来跟踪shell脚本中的命令的执行。这会让shell在终端上显示所有执行的命令及其参数 。
在Shell脚本中,我们可以使用多种方法来跟踪和调试命令的执行,这些方法可以帮助我们了解命令的执行过程,找出潜在的问题,并优化脚本性能,本文将介绍几种常用的跟踪调试方法,并提供相应的示例代码。

网站建设哪家好,找成都创新互联公司!专注于网页设计、网站建设、微信开发、成都微信小程序、集团企业网站建设等服务项目。为回馈新老客户创新互联还提供了句容免费建站欢迎大家使用!
1. 使用echo命令输出调试信息
在Shell脚本中,我们可以使用echo命令输出调试信息,这些信息可以在终端显示,帮助我们了解脚本的执行过程,我们可以在脚本的关键点添加echo语句,输出变量的值、函数的返回值等。
!/bin/bash
function add() {
local a=$1
local b=$2
echo "开始计算: $a + $b"
local sum=$((a + b))
echo "计算结果: $sum"
return $sum
}
result=add 3 5
echo "最终结果: $result"
在这个示例中,我们在add函数内部使用了echo命令输出计算过程和结果,运行这个脚本,我们可以看到如下输出:
开始计算: 3 + 5
计算结果: 8
最终结果: 8
2. 使用set -x命令启用调试模式
在Shell脚本中,我们可以使用set -x命令启用调试模式,这将在执行每个命令之前,先将其输出到标准错误(stderr),这样,我们可以实时查看脚本的执行过程,找出问题所在,要关闭调试模式,只需在脚本开头加上set +x命令即可。
!/bin/bash
set -e 设置脚本在遇到错误时立即退出
set -x 开启调试模式
function divide() {
local num=$1
echo "开始除法运算: $num"
if [ $num == 0 ]; then
echo "除数不能为0" >&2; exit 1
fi
local result=$((num / 2))
echo "除法运算结果: $result"
}
divide 4 2 >/dev/null 将除法运算结果重定向到/dev/null,避免干扰输出
运行这个脚本,我们可以看到如下输出:
$ bash script.sh
开始除法运算: 4
除法运算结果: 2
从输出中,我们可以看到除法运算的结果是正确的,我们还可以看到调试信息,如开始除法运算和除法运算结果等,这有助于我们了解脚本的执行过程。
3. 使用trap命令捕获信号并输出调试信息
在Shell脚本中,我们可以使用trap命令捕获信号并输出调试信息,当脚本接收到指定的信号时,trap命令会执行相应的操作,我们可以捕获SIGINT信号(通常由Ctrl+C产生),然后输出一条调试信息。
!/bin/bash
function show_help() {
echo "用法: $0 [选项]" >&2; exit 1; 将帮助信息输出到标准错误(stderr)并退出脚本
}
trap 'show_help' INT 当接收到SIGINT信号时,调用show_help函数并退出脚本
运行这个脚本,当我们按下Ctrl+C时,会看到以下输出:
“`textile –indent=tabs –font="Menlo,Monaco,Lucida Console,Liberation Mono,DejaVu Sans Mono,Bitstream Vera Sans Mono" –colorscheme=solarizeddark –background="272822" –foreground="cc6666" –line-numbers –margin=3 –padding=3 –header-color="999999" –titlebar="66cccc" –page=1 –base-font="Menlo,Monaco,Lucida Console,Liberation Mono,DejaVu Sans Mono,Bitstream Vera Sans Mono" –term-font="Menlo,Monaco,Lucida Console,Liberation Mono,DejaVu Sans Mono,Bitstream Vera Sans Mono" –code-font="Menlo,Monaco,Lucida Console,Liberation Mono" –header-color="eeeeee" –footer-color="333333" –header-char=’*’ –footer-char=’*’ –ruler-color=’aaaaaa’ –progress-indicator=’?’ –job-prefix='[bold][yellow]’ –job-suffix='[reset]’ –status-char='[bold][cyan]’ –progress-char=’.’ –output ‘| %F{white}|%f [%F{green}+%f%%][%F{red}] %E[%p%]: %m | %F{white}|%f | %F{green}[%f%%] %E[%p%] | %F{white}|%f’ –time-format ‘%D [%a %b %d %H:%M:%S %Y]’ –auto-quit –signals=SIGINT,SIGTERM 当接收到SIGTERM信号时(通常由kill命令产生),退出脚本并显示当前时间和运行时间统计信息。" "$0: line %N in
基本
文件
流程
错误
SQL
调试
- 请求信息 : 2026-02-16 21:22:35 HTTP/1.1 GET : /article/djppccs.html
- 运行时间 : 0.1133s ( Load:0.0047s Init:0.0010s Exec:0.1013s Template:0.0064s )
- 吞吐率 : 8.83req/s
- 内存开销 : 2,224.54 kb
- 查询信息 : 12 queries 5 writes
- 文件加载 : 36
- 缓存信息 : 0 gets 2 writes
- 配置加载 : 130
- 会话信息 : SESSION_ID=ag7mnbm00e7d7jej3ek27il002
- /home/wwwroot/jxjierui.cn/index.php ( 1.12 KB )
- /home/wwwroot/jxjierui.cn/ThinkPHP/ThinkPHP.php ( 4.61 KB )
- /home/wwwroot/jxjierui.cn/ThinkPHP/Library/Think/Think.class.php ( 12.26 KB )
- /home/wwwroot/jxjierui.cn/ThinkPHP/Library/Think/Storage.class.php ( 1.37 KB )
- /home/wwwroot/jxjierui.cn/ThinkPHP/Library/Think/Storage/Driver/File.class.php ( 3.52 KB )
- /home/wwwroot/jxjierui.cn/ThinkPHP/Mode/common.php ( 2.82 KB )
- /home/wwwroot/jxjierui.cn/ThinkPHP/Common/functions.php ( 53.56 KB )
- /home/wwwroot/jxjierui.cn/ThinkPHP/Library/Think/Hook.class.php ( 4.01 KB )
- /home/wwwroot/jxjierui.cn/ThinkPHP/Library/Think/App.class.php ( 13.49 KB )
- /home/wwwroot/jxjierui.cn/ThinkPHP/Library/Think/Dispatcher.class.php ( 14.79 KB )
- /home/wwwroot/jxjierui.cn/ThinkPHP/Library/Think/Route.class.php ( 13.36 KB )
- /home/wwwroot/jxjierui.cn/ThinkPHP/Library/Think/Controller.class.php ( 11.23 KB )
- /home/wwwroot/jxjierui.cn/ThinkPHP/Library/Think/View.class.php ( 7.59 KB )
- /home/wwwroot/jxjierui.cn/ThinkPHP/Library/Behavior/BuildLiteBehavior.class.php ( 3.68 KB )
- /home/wwwroot/jxjierui.cn/ThinkPHP/Library/Behavior/ParseTemplateBehavior.class.php ( 3.88 KB )
- /home/wwwroot/jxjierui.cn/ThinkPHP/Library/Behavior/ContentReplaceBehavior.class.php ( 1.91 KB )
- /home/wwwroot/jxjierui.cn/ThinkPHP/Conf/convention.php ( 11.15 KB )
- /home/wwwroot/jxjierui.cn/App/Common/Conf/config.php ( 2.12 KB )
- /home/wwwroot/jxjierui.cn/ThinkPHP/Lang/zh-cn.php ( 2.55 KB )
- /home/wwwroot/jxjierui.cn/ThinkPHP/Conf/debug.php ( 1.48 KB )
- /home/wwwroot/jxjierui.cn/App/Home/Conf/config.php ( 0.32 KB )
- /home/wwwroot/jxjierui.cn/App/Home/Common/function.php ( 3.33 KB )
- /home/wwwroot/jxjierui.cn/ThinkPHP/Library/Behavior/ReadHtmlCacheBehavior.class.php ( 5.62 KB )
- /home/wwwroot/jxjierui.cn/App/Home/Controller/ArticleController.class.php ( 6.11 KB )
- /home/wwwroot/jxjierui.cn/App/Home/Controller/CommController.class.php ( 1.60 KB )
- /home/wwwroot/jxjierui.cn/ThinkPHP/Library/Think/Model.class.php ( 60.11 KB )
- /home/wwwroot/jxjierui.cn/ThinkPHP/Library/Think/Db.class.php ( 32.43 KB )
- /home/wwwroot/jxjierui.cn/ThinkPHP/Library/Think/Db/Driver/Pdo.class.php ( 16.74 KB )
- /home/wwwroot/jxjierui.cn/ThinkPHP/Library/Think/Cache.class.php ( 3.83 KB )
- /home/wwwroot/jxjierui.cn/ThinkPHP/Library/Think/Cache/Driver/File.class.php ( 5.87 KB )
- /home/wwwroot/jxjierui.cn/ThinkPHP/Library/Think/Template.class.php ( 28.16 KB )
- /home/wwwroot/jxjierui.cn/ThinkPHP/Library/Think/Template/TagLib/Cx.class.php ( 22.40 KB )
- /home/wwwroot/jxjierui.cn/ThinkPHP/Library/Think/Template/TagLib.class.php ( 9.16 KB )
- /home/wwwroot/jxjierui.cn/App/Runtime/Cache/Home/7540f392f42b28b481b30614275e4e55.php ( 13.96 KB )
- /home/wwwroot/jxjierui.cn/ThinkPHP/Library/Behavior/WriteHtmlCacheBehavior.class.php ( 0.97 KB )
- /home/wwwroot/jxjierui.cn/ThinkPHP/Library/Behavior/ShowPageTraceBehavior.class.php ( 5.24 KB )
- [ app_init ] --START--
- Run Behavior\BuildLiteBehavior [ RunTime:0.000006s ]
- [ app_init ] --END-- [ RunTime:0.000046s ]
- [ app_begin ] --START--
- Run Behavior\ReadHtmlCacheBehavior [ RunTime:0.000294s ]
- [ app_begin ] --END-- [ RunTime:0.000331s ]
- [ view_parse ] --START--
- [ template_filter ] --START--
- Run Behavior\ContentReplaceBehavior [ RunTime:0.000067s ]
- [ template_filter ] --END-- [ RunTime:0.000088s ]
- Run Behavior\ParseTemplateBehavior [ RunTime:0.005024s ]
- [ view_parse ] --END-- [ RunTime:0.005053s ]
- [ view_filter ] --START--
- Run Behavior\WriteHtmlCacheBehavior [ RunTime:0.000097s ]
- [ view_filter ] --END-- [ RunTime:0.000109s ]
- [ app_end ] --START--
- 1064:You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near ') LIMIT 1' at line 1
[ SQL语句 ] : SELECT `id`,`pid`,`navname` FROM `cx_nav` WHERE ( id= ) LIMIT 1
- 1064:You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near ') LIMIT 1' at line 1
[ SQL语句 ] : SELECT `id`,`navname` FROM `cx_nav` WHERE ( id= ) LIMIT 1
- 1064:You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near ')' at line 1
[ SQL语句 ] : SELECT `id`,`navname` FROM `cx_nav` WHERE ( pid= )
- [8] Undefined index: pid /home/wwwroot/jxjierui.cn/App/Home/Controller/ArticleController.class.php 第 47 行.
- [2] file_put_contents(./App/Runtime/Temp/a1af4fee6b0f7a696d96b824a6f2c4b7.php): failed to open stream: Permission denied /home/wwwroot/jxjierui.cn/ThinkPHP/Library/Think/Cache/Driver/File.class.php 第 132 行.
- [8] Undefined index: db_host /home/wwwroot/jxjierui.cn/ThinkPHP/Library/Think/Db.class.php 第 120 行.
- [8] Undefined index: db_port /home/wwwroot/jxjierui.cn/ThinkPHP/Library/Think/Db.class.php 第 121 行.
- [8] Undefined index: db_name /home/wwwroot/jxjierui.cn/ThinkPHP/Library/Think/Db.class.php 第 122 行.
- [2] file_put_contents(./App/Runtime/Temp/ffe585e30995add4a2c236eac3ea1b26.php): failed to open stream: Permission denied /home/wwwroot/jxjierui.cn/ThinkPHP/Library/Think/Cache/Driver/File.class.php 第 132 行.

0.1133s
