【问题标题】:ZSH script and prompt profiling?ZSH 脚本和提示分析?
【发布时间】:2012-02-12 13:37:27
【问题描述】:

这个答案“How to profile a bash shell script?”似乎几乎完美地涵盖了我在这里想要完成的任务。我目前有一些修改提示的 zsh 脚本,但是我认为对 oh-my-zsh 的一些更新引起了一些我需要追查的问题。时不时的呆滞让人难以忍受。

为此,您将如何调整此示例答案中的提示部分以使用 zsh 与 bash?

目前我已经修改了/etc/zshenv,使其具有示例中的初始建议代码:

PS4='+ $(date "+%s.%N")\011 '
exec 3>&2 2>/tmp/bashstart.$$.log
set -x

我的~/.zshrc 的尾部附加了以下内容:

set +x
exec 2>&3 3>&-

当然,这些对 ZSH shell 自定义无效。我的提示渲染代码使用了 oh-my-zsh 自定义。我可以在我想的提示中添加适当的代码,或者我愿意接受其他建议。

【问题讨论】:

  • 请考虑编辑您的帖子以包含您认为相关的“此示例答案中的提示部分”,并指出它现在不适合您的地方。祝你好运。
  • 基本上我只需要将这些转换为 zsh 等效项,以便在我搜索自定义项后预先提示我。 :D 我已经有大约一年没有自定义它了,所以我必须在 oh-my-zsh 自定义中挖掘部分备份。

标签: bash zsh zshrc


【解决方案1】:

为每个命令调用 date 将分叉和执行,这会增加开销,可能会干扰您的测量。

相反,您可以使用

PS4=$'+ %D{%s.%6.}\011 '

以较低的开销记录时间戳(精度高达毫秒)。

有关处理结果日志的一些说明,请参阅http://blog.xebia.com/profiling-zsh-shell-scripts/

【讨论】:

    【解决方案2】:

    你可能需要这样做

    setopt prompt_subst
    

    如果还没有的话。

    另外,为了解释制表符的八进制转义,请使用$''

    PS4=$'+ $(date "+%s.%N")\011 '
    

    您可能还会发现其中一些转义很有用:

       %?     The return status of the last command executed just before the prompt.
    
       %_     The  status  of  the parser, i.e. the shell constructs (like `if' and `for') that have been started on the command
              line. If given an integer number that many strings will be printed; zero or negative or no integer means print  as
              many  as  there  are.   This  is  most useful in prompts PS2 for continuation lines and PS4 for debugging with the
              XTRACE option; in the latter case it will also work non-interactively.
    
       %i     The line number currently being executed in the script, sourced file, or shell function given by %N.  This is most
              useful for debugging as part of $PS4.
    
       %I     The line number currently being executed in the file %x.  This is similar to %i, but the line number is  always  a
              line number in the file where the code was defined, even if the code is a shell function.
    
       %L     The current value of $SHLVL.
    
       %N     The  name  of  the  script, sourced file, or shell function that zsh is currently executing, whichever was started
              most recently.  If there is none, this is equivalent to the parameter $0.  An integer may follow the `%' to  spec‐
              ify  a number of trailing path components to show; zero means the full path.  A negative integer specifies leading
              components.
    
       %x     The name of the file containing the source code currently being executed.  This behaves as %N except that function
              and eval command names are not shown, instead the file where they were defined.
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2013-02-16
      • 2015-07-23
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-09-06
      相关资源
      最近更新 更多