previous命令失败后创建一个红色的$ bash命令提示符 [英] Creating a Bash command prompt with a red $ after failure of previous command

查看:229
本文介绍了previous命令失败后创建一个红色的$ bash命令提示符的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是新来bash编程,而我正在创建一个自定义bash命令提示符。我的目标是创建一个提示,这只能说明登录名和主机名,当他们从我通常使用不同。我也期待追加当前的Git分支到命令提示符的目录是Git版本控制之下时。

I'm new to Bash programming, and I'm working on creating a custom Bash command prompt. My goal is to create a prompt which only shows the login name and host name when they differ from what I normally use. I'm also looking to append the current Git branch to the command prompt when in a directory which is under Git version control.

我想色彩的登录名和主机名部分绿色,目录路径蓝色,Git的分支部分的粉红色,和分隔符(:和$字符)白色。然而,当previously执行的命令返回非零什么,我想上色$分隔红。没有颜色的一般格式如下:

I would like to color the login and host name section green, the directory path blue, the Git branch section pink, and separators (: and $ characters) white. However, when the previously executed command returns anything other than zero, I would like to color the $ separator red. The general format without colors looks like this:

loginname@hostname:~/current/path:branchname$

这是强制性的唯一部分是目录路径和$字符。这里的code,我写为我的的.bashrc 文件:

The only sections that are mandatory are the directory path and the $ character. Here's the code I've written for my .bashrc file:

MYNAME="gwen"
MYHOST="gwen-laptop"

RED="\[\033[31m\]"
WHITE="\[\033[0m\]"
GREEN="\[\033[01;32m\]"
BLUE="\[\033[01;34m\]"
PINK="\[\033[01;35m\]"

DOLLAR="if [ \$? = 0 ]; then echo ${WHITE}\$; else echo ${RED}\$${NORMAL}; fi"
GITBRN='$(__git_ps1 "\033[0m:\033[01;35m%s")'

USERNM="if [ \u != ${MYNAME} ]; then whoami; fi;"
HOSTNM="if [ \h != ${MYHOST} ]; then echo -n @; hostname; fi;"
COLONM="if [ \u != ${MYNAME} ] || [ \h != ${MYHOST} ]; then echo -n :; fi;"

PS1="${GREEN}\`${USERNM}\`\`${HOSTNM}\`${WHITE}\`${COLONM}\`${BLUE}\w${GITBRN}\`${DOLLAR}\` "

这code满足我所有的要求,除了它离开$字符在任何时候都白,不色红是在适当的时间即可。 (我怀疑的问题是,\\ $?在DOLLAR引用了previously执行的命令,而是构建PS1当最后一块钱被执行,所以previously执行语句不再这是之前运行命令PS1开始施工;它的东西,这是为了创建命令提示符下执行)我的不知道如何解决这个问题

This code meets all of my requirements, except that it leaves the $ character white at all times, and does not color it red at the appropriate times. (I suspect the problem is that the "\$?" in DOLLAR references the previously executed command, but DOLLAR is executed last when constructing PS1, so the previously execute statement is no longer the command which was run before PS1 construction began; it's something which was executed in order to create the command prompt.) I'm not sure how to solve this problem.

这code是丑陋和需要进行重构。我试图将所有的颜色codeS到他们自己的变量,但是当我在code为GITBRN用这些颜色变量,事情失控了,所以我结束了使用文字的颜色了,而不是。

This code is ugly and needs to be refactored. I was trying to move all the color codes into their own variables, but when I used these color variables in the code for GITBRN, thing went haywire, so I ended up using literal colors there instead.

我已经花了一整天试图让这个code的工作,我想我无处可去在这一点上。对于如何获得红色的美元符号在适当的时候将是最AP preciated 的的建议。我也开放给建议在重构code,使之更清洁,更具有可读性。

I've spent an entire day trying to get this code working, and I think I'm going nowhere at this point. Any suggestions for how to get that dollar sign colored red at the appropriate time would be most appreciated. I'm also open to suggestion on refactoring the code to make it cleaner and more readable.

P.S。我是一个Ubuntu Linux操作系统(清醒山猫)用户。

P.S. I'm a Ubuntu Linux (Lucid Lynx) user.

推荐答案

根据bash的手册页使用 PROMPT_COMMAND 变量,这是每一个主提示符之前执行。

Use the PROMPT_COMMAND variable, which is executed before each primary prompt according to the bash man page.

例如(这还不工作,我试图得到它的工作的权利,但我认为这是可能的):

For example (this doesn't work yet, I'm trying to get it working right, but I think it's possible):

PROMPT_COMMAND="if [ \$? = 0 ]; then DOLLAR="${WHITE}\$${NORMAL}"; else DOLLAR="${RED}\$${NORMAL}"; fi"


编辑:由于与执行命令和非打印字符内 PS1挫折(即 \\ [ \\] 有时会打印出从字面上,而不是用来作为提示, PS1 ),我来与此(替换 ... 与任何你想在你的提示):


due to frustrations with executing commands and nonprinting characters inside PS1 (the \[ and \] sometimes get printed out literally instead of used as hints to PS1), I've come up with this (replace ... with whatever you want in your prompt):

PROMPT_COMMAND='if [ $? = 0 ]; then DOLLAR_COLOR="\033[0m"; else DOLLAR_COLOR="\033[31m"; fi'
PS1='...\[$(echo -ne $DOLLAR_COLOR)\]$\[\033[m\] '

当然,使用 $(),你可以把你的内心 PS1喜欢的任何部分本,而不是使用 PROMPT_COMMAND ,我只是喜欢这种方式,使 PROMPT_COMMAND 包含逻辑和 PS1 包含显示命令。

Of course, using $() you could put whichever parts of this you like inside PS1 instead of using PROMPT_COMMAND, I just like it this way so that PROMPT_COMMAND contains the logic and PS1 contains the display commands.

这篇关于previous命令失败后创建一个红色的$ bash命令提示符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆