bash登录远程服务器
If you access multiple servers throughout the work day, then being able to tell which one you are working with at a glance based on a color scheme can be very useful. Today’s SuperUser Q&A post helps a reader achieve a colorful nirvana in his work environment.
如果您在一整天的工作中访问多台服务器,那么根据颜色方案一目了然就可以知道正在使用哪台服务器,这将非常有用。 今天的超级用户问答环节可帮助读者在工作环境中实现丰富多彩的必杀技。
Today’s Question & Answer session comes to us courtesy of SuperUser—a subdivision of Stack Exchange, a community-driven grouping of Q&A web sites.
今天的“问答”环节由SuperUser提供,它是Stack Exchange的一个分支,该社区是由社区驱动的Q&A网站分组。
问题 (The Question)
SuperUser reader nitins wants to know how to make the Bash Prompt change colors when logged into a server:
超级用户阅读器nitins想知道如何在登录服务器时使Bash Prompt更改颜色:
Is there a way to make the Bash Prompt dynamic so that it changes color when I am logged into a server? I want the color to be green when using my own personal system, but change to red whenever I am connected to servers. I have a huge number of servers that I access and do not want to have to put a different .bashrc on each and every one of them.
有没有一种方法可以使Bash Prompt动态化,以便在我登录服务器时可以更改颜色? 使用自己的个人系统时,我希望颜色为绿色,但是当我连接到服务器时,颜色会变为红色。 我有大量的服务器可以访问,并且不想在每个服务器上都放置不同的.bashrc 。
How do you make the Bash Prompt change colors when logged into a server?
登录服务器时如何使Bash Prompt更改颜色?
答案 (The Answer)
SuperUser contributor Matei David has the answer for us:
超级用户贡献者Matei David为我们提供了答案:
Remote Bash Prompts are set by the remote ~/.bashrc, so you will still need to copy it to the remote servers. However, you can use a single ~/.bashrc for all hosts and set the Bash Prompt color based on the host name:
远程Bash提示由远程〜/ .bashrc设置 ,因此您仍然需要将其复制到远程服务器。 但是,您可以对所有主机使用单个〜/ .bashrc并根据主机名设置Bash Prompt颜色:
Notes
笔记
Do not set PS1 if it is not already set (i.e. if the shell is not interactive). Testing to see if PS1 is “non-empty” is a very common way to find out if the shell is interactive and you do not want to confuse programs that do that (arguably, a more accurate test is checking to see if $- contains i).
如果尚未设置PS1,则不要设置它(即,如果外壳不是交互式的)。 测试PS1是否为“非空”是一种很常见的方法,可确定外壳是否是交互式的,并且您不想混淆执行该操作的程序(可以说,更准确的测试是检查$-是否包含我 )。
If you want this code to run when logging into a remote server, you should have one of the profile files always source ~/.bashrc (I am assuming you know that).
如果要让此代码在登录到远程服务器时运行,则应该让配置文件之一始终作为源〜/ .bashrc (我假设您知道)。
In PS1, the escape codes must be enclosed in \[…\].
在PS1中 ,转义代码必须包含在\ […\]中 。
\[033[m resets the foreground and background to their defaults, so here :\w appears in the terminal foreground/background.
\ [033 [m将前景和背景重置为默认值,因此:\ w显示在终端的前景/背景中。
\[033[48;5;XXXm\033[38;5;YYYm sets the background/foreground to XXX/YYY.
\ [033 [48; 5; XXXm \ 033 [38; 5; YYYm]将背景/前景设置为XXX / YYY 。
- For a script that dumps the available colors, try colortest. 对于转储可用颜色的脚本,请尝试colortest。
To check and see what the Bash Prompt would look like, use: echo -e “<\033[48;5;16m\033[38;5;196mhost\033[m:dir>$ “
要检查并查看Bash Prompt的外观,请使用: echo -e“ <\ 033 [48; 5; 16m \ 033 [38; 5; 196mhost \ 033 [m:dir> $”
Have something to add to the explanation? Sound off in the comments. Want to read more answers from other tech-savvy Stack Exchange users? Check out the full discussion thread here.
有什么补充说明吗? 在评论中听起来不错。 是否想从其他精通Stack Exchange的用户那里获得更多答案? 在此处查看完整的讨论线程 。
Image Credit: Emx (Wikipedia)
图片来源: Emx(Wikipedia)
bash登录远程服务器