Bash 生成特定于机器的PS1提示符的可维护方法
为了帮助自己记住,我快速、直观地登录到了不同的系统,我在Bash环境变量中对字段使用了不同的颜色 我正在寻找一种可靠地生成绑定到静态主机标识符的Bash 生成特定于机器的PS1提示符的可维护方法,bash,terminal,xterm,ps1,terminal-emulator,Bash,Terminal,Xterm,Ps1,Terminal Emulator,为了帮助自己记住,我快速、直观地登录到了不同的系统,我在Bash环境变量中对字段使用了不同的颜色 我正在寻找一种可靠地生成绑定到静态主机标识符的PS1的方法;比如说,通过命令 最初,我的PS1是根据生成的内容改编的,看起来像: export PS1="\u@\[$(tput bold)\]\[$(tput sgr0)\]\[\033[38;5;35m\]\h\[$(tput sgr0)\]\[$(tput sgr0)\]\[\033[38;5;15m\]\[$(tput sgr0)\]:\[\0
PS1
的方法;比如说,通过命令
最初,我的PS1
是根据生成的内容改编的,看起来像:
export PS1="\u@\[$(tput bold)\]\[$(tput sgr0)\]\[\033[38;5;35m\]\h\[$(tput sgr0)\]\[$(tput sgr0)\]\[\033[38;5;15m\]\[$(tput sgr0)\]:\[\033[38;5;245m\]\w\[$(tput sgr0)\]\[\033[38;5;15m\]\[$(tput sgr0)\]\\$ "
真是一团糟
因此,要开始任何进展,第一步是进行一些重构。这让我想到了以下脚本:
bold="$(tput bold)"
reset="$(tput sgr0)"
green="\e[38;5;35m"
gray="\e[38;5;245m"
directory='\w'
host='\h'
user='\u'
function colorize() {
echo -n "${2}${1}${reset}"
}
export PS1="${user}@$(colorize $host $green):$(colorize $directory $gray)\\$ "
在这一点上,你至少可以看到到底发生了什么
现在,我需要编写一个函数,如:
get_repeatable_color_for_hostid() {
# hash the $(hostid) into a valid color escape string
# e.g. 16ab1d60 --> \e[38;5;35m
}
为此,我需要了解:
- 字段部分
内部,例如xx的含义是什么;Yzzm
\e[38;5;35m
- 如何从
到颜色转义序列进行散列,s.t.颜色尽可能随机化hostid
还可以使用主机名的校验和生成颜色(从1到256):
color=$($(主机名|校验和| cut-f1-d“”)%256+1))
太酷了!那个链接错误地识别了狮子的份额。在我想知道神奇数字38
和5
的时候?这只是个小细节。我已经实现了一些与你的建议非常相似的东西,并将其检查到我的dot文件中: