有没有办法调试Bash脚本?
例如,打印某种执行日志的东西,如“呼叫线路1”、“呼叫线路2”等。
发布于 2009-06-04 16:22:34
我使用以下方法来调试我的脚本。
如果任何外部程序返回非零退出状态,set -e
会使脚本立即停止。如果您的脚本试图处理所有错误情况,并且应捕获失败的情况,则此功能非常有用。
上面提到了set -x
,它肯定是所有调试方法中最有用的。
如果您想要检查脚本中的语法错误,set -n
可能也很有用。
strace
对于查看正在发生的事情也很有用。如果您还没有自己编写脚本,这一点尤其有用。
发布于 2014-09-10 15:23:48
我发现了shellcheck实用程序,也许有些人会觉得它很有趣。
下面是一个小示例:
$ cat test.sh
ARRAY=("hello there" world)
for x in $ARRAY; do
echo $x
done
$ shellcheck test.sh
In test.sh line 3:
for x in $ARRAY; do
^-- SC2128: Expanding an array without an index only gives the first element.
修复bug。先试一试...
$ cat test.sh
ARRAY=("hello there" world)
for x in ${ARRAY[@]}; do
echo $x
done
$ shellcheck test.sh
In test.sh line 3:
for x in ${ARRAY[@]}; do
^-- SC2068: Double quote array expansions, otherwise they're like $* and break on spaces.
我们再试一次..。
$ cat test.sh
ARRAY=("hello there" world)
for x in "${ARRAY[@]}"; do
echo $x
done
$ shellcheck test.sh
现在找到了!
这只是一个小例子。
发布于 2009-06-04 15:43:17
您还可以在脚本中编写"set -x“。
https://stackoverflow.com/questions/951336
复制相似问题