【发布时间】:2019-06-25 22:17:38
【问题描述】:
我是一名 Windows 用户,我使用 Git Bash shell 作为我的日常驱动程序。我很好奇__git_ps1 函数是如何在您每次更改目录时更新提示的。这确实是我见过的即时更新 bash 提示的唯一示例。如果我打开了 RDP 会话,我想在我自己的函数中利用这种行为来在我的提示符上添加一个显示。
tldr:关于 __git_ps1 函数如何动态评估 bash 提示的任何想法????
所以这是我查看 RDP 客户端是否正在运行的简单函数
function __rdp_ps1() {
local MATCH=
if tasklist | grep --quiet mstsc; then
MATCH="\e[41mRDP\e[0m"
fi
echo "$MATCH"
}
所以我的想法是我想用红色背景显示 RDP,并且我希望我的 shell 以 __git__ps1 看似能够的方式即时评估它。
到目前为止我所调查的(没有真正成功)
/etc/profile.d/git-prompt.sh
这个块似乎创建了我的 shell 正在使用的 PS1
PS1='\[\033]0;$TITLEPREFIX:$PWD\007\]' # set window title
PS1="$PS1"'\n' # new line
PS1="$PS1"'\[\033[32m\]' # change to green
PS1="$PS1"'\u@\h ' # user@host<space>
PS1="$PS1"'\[\033[35m\]' # change to purple
PS1="$PS1"'$MSYSTEM ' # show MSYSTEM
PS1="$PS1"'\[\033[33m\]' # change to brownish yellow
PS1="$PS1"'\w' # current working directory
if test -z "$WINELOADERNOEXEC"
then
GIT_EXEC_PATH="$(git --exec-path 2>/dev/null)"
COMPLETION_PATH="${GIT_EXEC_PATH%/libexec/git-core}"
COMPLETION_PATH="${COMPLETION_PATH%/lib/git-core}"
COMPLETION_PATH="$COMPLETION_PATH/share/git/completion"
if test -f "$COMPLETION_PATH/git-prompt.sh"
then
. "$COMPLETION_PATH/git-completion.bash"
. "$COMPLETION_PATH/git-prompt.sh"
PS1="$PS1"'\[\033[36m\]' # change color to cyan
# 在这里尝试了 hamjamming PS1="$PS1 `__rdp_ps1`",它只在登录时有效
PS1="$PS1"'`__git_ps1`' # bash function
fi
fi
PS1="$PS1"'\[\033[0m\]' # change color
PS1="$PS1"'\n' # new line
PS1="$PS1"'$ ' # prompt: always $
所以我去看看这个文件的来源,看看是否能找到答案
/etc/bash.bashrc
最后一行是金牌
# Fixup git-bash in non login env
shopt -q login_shell || . /etc/profile.d/git-prompt.sh`
所以我评估了shopt login_shell 并且它总是打开,但我真的不知道这意味着什么,因为评论让我相信当登录 env 关闭时,提示脚本将被评估
有什么想法吗???
【问题讨论】:
-
@melpomene 我不明白你在说什么——我正在使用命令替换并且我得到了正确的显示,它只是在登录时被评估。你能解释一下吗?
-
您在问题中没有提到这一点。你在哪里/如何设置你的
PS1来调用__rdp_ps1? -
# 在这里尝试了 hamjamming PS1="$PS1
__rdp_ps1",它只在登录时有效