Bash 设置PROMPT_命令和PS4(在命令启动时有日期,打印路径复杂)

Bash 设置PROMPT_命令和PS4(在命令启动时有日期,打印路径复杂),bash,command-prompt,prompt,ps1,Bash,Command Prompt,Prompt,Ps1,我正在尝试显示命令的执行日期。因此,我使用ps4bash环境变量 什么给了我以下提示 [user@host temp]$ \ls :2012-04-10 13:43:52: ls dir1 dir12 test [user@host temp]$ 另一方面,当我在一个深目录中时,我希望我的路径不要太长(这是经常说的)。我发现下面的代码(我不记得在哪里)非常好 bash_prompt_command() { # How many characters of the $PWD should b

我正在尝试显示命令的执行日期。因此,我使用ps4bash环境变量

什么给了我以下提示

[user@host temp]$ \ls
:2012-04-10 13:43:52: ls
dir1  dir12  test
[user@host temp]$
另一方面,当我在一个深目录中时,我希望我的路径不要太长(这是经常说的)。我发现下面的代码(我不记得在哪里)非常好

bash_prompt_command() {
# How many characters of the $PWD should be kept
local pwdmaxlen=25
# Indicate that there has been dir truncation
local trunc_symbol=".."
local dir=${PWD##*/}
pwdmaxlen=$(( ( pwdmaxlen < ${#dir} ) ? ${#dir} : pwdmaxlen ))
NEW_PWD=${PWD/#$HOME/\~}
local pwdoffset=$(( ${#NEW_PWD} - pwdmaxlen ))
if [ ${pwdoffset} -gt "0" ]
then
    NEW_PWD=${NEW_PWD:$pwdoffset:$pwdmaxlen}
    NEW_PWD=${trunc_symbol}/${NEW_PWD#*/}
fi
}
PS1="[\u@\h \${NEW_PWD}]\\$ "
PROMPT_COMMAND=bash_prompt_command
当我同时使用PS4和PROMPT_命令时,问题就出现了。是什么让我感觉到了可怕的事情:

[user@host temp]$ \ls
:2012-04-10 13:48:32: ls
dir1  dir12  test
::2012-04-10 13:48:32: bash_prompt_command
::2012-04-10 13:48:32: local pwdmaxlen=25
::2012-04-10 13:48:32: local trunc_symbol=..
::2012-04-10 13:48:32: local dir=temp
::2012-04-10 13:48:32: pwdmaxlen=25
::2012-04-10 13:48:32: NEW_PWD='~/temp'
::2012-04-10 13:48:32: local pwdoffset=-19
::2012-04-10 13:48:32: '[' -19 -gt 0 ']'
[user@host temp]$ 
提示命令功能中使用的命令由PS4显示

我正在寻找一种避免这种影响的方法:

  • 通过在我的提示(PS1或prompt_命令)中显示实时(在同一提示行上每秒更新一次)
  • 找到另一种减少路径长度的方法(当我不在~,我需要打印最后两个目录)
  • 也许还有其他想法

我知道这是一个棘手的问题,但我认为BASH应该能够做我想做的事情

最简单的方法是在每个命令之前执行代码

function tsprint() {
        if [[ $BASH_COMMAND != bash_prompt_command ]]
        then
                echo $(date) ": $BASH_COMMAND"
        fi
}
PS1="[\u@\h \${NEW_PWD}]\\$ "
PROMPT_COMMAND="bash_prompt_command;trap 'tsprint; trap DEBUG' DEBUG"
下面是示例输出:

[shaman@shamanbook ~]$ cd Music/
Fri Apr 13 02:22:34 EEST 2012 : cd Music/
[shaman@shamanbook ~]$ ls
Fri Apr 13 02:22:34 EEST 2012 : ls --color=auto
[shaman@shamanbook ~/Music]$
[shaman@shamanbook ~/Music]$ pwd
Fri Apr 13 02:22:39 EEST 2012 : pwd
/home/shaman/Music
[shaman@shamanbook ~/Music]$
[shaman@shamanbook ~/Music]$

这是一个好的开始。。。问题在于,在执行命令之前会更新打印路径,这使得在执行
cd
命令之后打印路径错误。你有办法解决这个问题吗?我想我解决这个问题的方法是将
bash\u prompt\u命令直接放在
prompt\u命令中,而不是放在
trap
(我编辑你的答案)。我为我工作,你觉得怎么样?
function tsprint() {
        if [[ $BASH_COMMAND != bash_prompt_command ]]
        then
                echo $(date) ": $BASH_COMMAND"
        fi
}
PS1="[\u@\h \${NEW_PWD}]\\$ "
PROMPT_COMMAND="bash_prompt_command;trap 'tsprint; trap DEBUG' DEBUG"
[shaman@shamanbook ~]$ cd Music/
Fri Apr 13 02:22:34 EEST 2012 : cd Music/
[shaman@shamanbook ~]$ ls
Fri Apr 13 02:22:34 EEST 2012 : ls --color=auto
[shaman@shamanbook ~/Music]$
[shaman@shamanbook ~/Music]$ pwd
Fri Apr 13 02:22:39 EEST 2012 : pwd
/home/shaman/Music
[shaman@shamanbook ~/Music]$
[shaman@shamanbook ~/Music]$