6

这个答案“如何分析 bash shell 脚本? ”似乎几乎完美地涵盖了我在这里想要完成的任务。我目前有一些修改提示的 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 自定义。我可以在我想的提示之前添加适当的代码,或者我愿意接受其他建议。

4

2 回答 2

4

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

相反,您可以使用

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

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

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

于 2016-01-09T18:03:54.923 回答
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.
于 2012-01-18T17:18:13.150 回答