简体   繁体   English

Bash提示使用if [$? = 0]; 然后; else和/ $不按照它的意思运行

[英]Bash Prompt using if [ $? = 0 ]; then; else and /$ doesn't work the way it's meant

I know I already had this running quite some time ago, but I haven't messed around with scripts and bash prompts for a long time an I am unable to come up with a solution. 我知道我已经在很久以前就已经开始运行,但是我已经很长时间没有弄乱脚本和bash提示了我无法提出解决方案。

I tried to use if [ $? 我尝试使用if [$? = 0 ]; = 0]; then; 然后; else;, in order to become a different prompt if a command provides an error and \\$, in order to get a #, in case root is logged in. I even came up with this nonsensical long prompt command: else;,如果命令提供错误而成为一个不同的提示符和\\ $,以便获取#,以防root登录。我甚至想出了这个荒谬的长提示命令:

PROMPT_COMMAND='if [ $? = 0 ]; then PS1="\[\e[31m\]║\[\033[04;35m\]\$(/bin/date)\[\e[00;32m\] History: \! \[\e[31m\]Jobs: \j \[\e[34m\]Status: \[\e[32m\]▇▇  ${debian_chroot:+($debian_chroot)}\n\[\033[31m\]║\[\e[33m\]\u@\[\e[37m\]\w/\[\e[32m\]\$:\[\e[0m\]"; else PS1="\[\e[31m\]║\[\033[04;35m\]\$(/bin/date)\[\e[00;32m\] History: \! \[\e[31m\]Jobs: \j \[\e[34m\]Status: \[\e[31m\]▇▇ ${debian_chroot:+($debian_chroot)}\n\[\e[31m\]║\[\e[33m\]\u@\[\e[37m\]\w/\[\e[32m\]\$:\[\e[0m\]"; fi'

I know the problem is that the if [ $? 我知道问题是if [$? = 0 ]; = 0]; then; 然后; else; 其他; portion works between "", while the /$ seems to work only between ''. 部分在“”之间工作,而/ $似乎只在''之间起作用。

The funny thing is that this works ... 有趣的是,这有效......

PS1="\`if [ \$? = 0 ]; then echo \[\e[33m\]^_^\[\e[0m\]; else echo \[\e[31m\]O_O\[\e[0m\]; fi\`[\u@\h:\w]\\$ "

while this doesn't (the if [ \\$? = 0 ]; then; else;) (\\$(/bin/date) and \\$ works this way) 虽然这不是(if [\\ $?= 0];然后; else;)(\\ $(/ bin / date)和\\ $这样工作)

PS1="\[\033[31m\]║\[\033[04;35m\]\$(/bin/date)\[\033[00;32m\] History: \! \[\033[31m\]Jobs: \j \`if [ \$? = 0 ]; then echo \[\e[34m\]Status: \[\e[32m\]▇▇; else echo \[\e[34m\]Status: \[\e[31m\]▇▇; fi\` ${debian_chroot:+($debian_chroot)}\n\[\033[31m\]║\[\033[33m\]\u@\[\033[37m\]\w/\[\033[32m\]\\$:\[\033[0m\]"

I'd like to keep both in my prompt, but I am obviously missing something (maybe because I was looking at this prompt too much. I would be glad to have a prompt that features \\$ and \\$(/bin/date), while changing if an error occurs, please help. 我想在我的提示中保留两者,但我显然遗漏了一些东西(也许是因为我太过看了这个提示。我很高兴有一个提示功能\\ $和\\ $(/ bin / date) ,如果发生错误则更改,请帮忙。

Cheers. 干杯。

I recommend building your prompt from PROMPT_COMMAND , rather than trying to cram executable code into the value of PS1 itself. 我建议从PROMPT_COMMAND构建您的提示,而不是尝试将可执行代码塞入PS1本身的值。 This makes the quoting simpler, as well as breaking the prompt into manageable pieces. 这使得引用更简单,并且将提示分解为可管理的部分。

prompt_cmd () {
  exit_status=$?
  PS1='\[\e[31m\]║\[\e[04;35m\]\D{%+}'
  PS1+=' \[\e[32m\]History: \!'
  PS1+=' \[\e[31m\]Jobs: \j'
  PS1+=' \[\e[34m\]Status: '
  if [[ $exit_status = 0 ]]; then
    PS1+='\[\e[32m\]'
  else
    PS1+='\[\e[31m\]'
  fi
  PS1+='▇▇ '"${debian_chroot:+($debian_chroot)}"'\n'
  PS1+='\[\e[31m\]║\[\e[33m\]\u@\[\e[37m\]\w\[\e[32m\]\$:\[\e[0m\]'
}

PROMPT_COMMAND='prompt_cmd'

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM