首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >设置-x`调试我的`.zshrc`

设置-x`调试我的`.zshrc`
EN

Unix & Linux用户
提问于 2020-07-04 16:29:07
回答 2查看 5.2K关注 0票数 5

当我使用set -x调试命令时,它会输出一些额外的行。

代码语言:javascript
复制
% set -x
+precmd_update_git_vars:1> [ -n '' ']'
+precmd_update_git_vars:1> [ '!' -n '' ']'
+precmd_update_git_vars:2> update_current_git_vars
+update_current_git_vars:1> unset __CURRENT_GIT_STATUS
+update_current_git_vars:3> [[ python == python ]]
+update_current_git_vars:4> _GIT_STATUS=+update_current_git_vars:4> python /home/ismail/zshfiles/gitstatus.py
+update_current_git_vars:4> _GIT_STATUS='' 
+update_current_git_vars:6> __CURRENT_GIT_STATUS=( '' ) 
+update_current_git_vars:7> GIT_BRANCH='' 
+update_current_git_vars:8> GIT_AHEAD='' 
+update_current_git_vars:9> GIT_BEHIND='' 
+update_current_git_vars:10> GIT_STAGED='' 
+update_current_git_vars:11> GIT_CONFLICTS='' 
+update_current_git_vars:12> GIT_CHANGED='' 
+update_current_git_vars:13> GIT_UNTRACKED='' 
+precmd_update_git_vars:3> unset __EXECUTED_GIT_COMMAND

由于这些输出,我无法调试命令。

为什么set -x要调试我的.zshrc?我希望set -x只调试后面跟着set -x的行。

EN

回答 2

Unix & Linux用户

回答已采纳

发布于 2020-07-05 15:12:09

如果试图调试脚本,则不要在终端上使用set -x (即调试终端中运行的外壳)。相反,您可以使用解释器的-x选项启动脚本(例如,zsh -x [])。

例如,如果您有一个名为ex.zsh的zsh脚本,那么您可以这样做:

代码语言:javascript
复制
$ cat /tmp/ex.zsh
#!/bin/zsh

function () {
    echo "Hello, world!"
}
$ zsh -x /tmp/ex.zsh
+/tmp/ex.zsh:3> '(anon)'
+(anon):1> echo 'Hello, world!'
Hello, world!
票数 1
EN

Unix & Linux用户

发布于 2023-02-01 01:54:33

在shell中运行set -x会将“一切”置于调试模式中。但是,如果您只想调试一个shell函数,以及它调用的任何东西,您可以使用typeset -ft some_func来标记该函数,以便进行调试。例如:

代码语言:javascript
复制
$ which some_func
some_func() {
   echo "hiii"
   ls -ls
   some_other_func 
}
$ typeset -ft some_func
$ some_func
....

...

这将使调试输出范围仅限于您所关心的代码。只需运行typeset -f +t some_func就可以关闭调试。如果您想调试一个独立于您的环境的独立脚本,那么就像前面提到的那样,用zsh -x script.sh运行它。

票数 0
EN
页面原文内容由Unix & Linux提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://unix.stackexchange.com/questions/596715

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档