我正在使用Git,我已经更改了以下行.bashrc
,要在提示中显示当前的checkout分支,何时pwd
是Git Repo.我正在使用的操作系统是:Ubuntu 32bit
# Original PS1 Line
PS1='${debian_chroot:+($debian_chroot)}\[\033[01;32m\]\u@\h\[\033[00m\]:\[\033[01;34m\]\w\[\033[00m\]\$ '
Run Code Online (Sandbox Code Playgroud)
我正在使用此行在shell提示符中显示git repo的当前分支,而不是上面的行.
# PS1 Line to show current Git Branch in the Prompt
PS1='${debian_chroot:+($debian_chroot)}\[\033[01;32m\]\u@\h\[\033[00m\]:\[\033[01;34m\]\w\[\033[00m\]\[\033[01;32m\]$(__git_ps1 " (%s)")\[\033[00m\]\$ '
Run Code Online (Sandbox Code Playgroud)
问题是当我把它交给朋友时,Shell会__git_ps1: command not found
在目录之间导航时出错,因为脚本会在更改目录时检查git分支.如何检查是否安装了Git并仅在安装了git时执行分支检查?
编辑: 正如ayckoster建议的那样,我想出了以下几行代码:
if [ "$color_prompt" = yes ]; then
git --version
GIT_IS_AVAILABLE=$?
if [ $GIT_IS_AVAILABLE -eq 0 ]; then
# PS1 Line to show current Git Branch in the Prompt
PS1='${debian_chroot:+($debian_chroot)}\[\033[01;32m\]\u@\h\[\033[00m\]:\[\033[01;34m\]\w\[\033[00m\]\[\033[01;32m\]$(__git_ps1 " (%s)")\[\033[00m\]\$ '
else
# Original PS1 Line
PS1='${debian_chroot:+($debian_chroot)}\[\033[01;32m\]\u@\h\[\033[00m\]:\[\033[01;34m\]\w\[\033[00m\]\$ '
fi
else
PS1='${debian_chroot:+($debian_chroot)}\u@\h:\w\$ '
fi
Run Code Online (Sandbox Code Playgroud)
现在,每当我打开终端时,我得到git --version
输出到屏幕,而安装了Git,我得到以下错误,而在未安装Git时打开终端:
The program 'git' is currently not installed. You can install it by typing:
sudo apt-get install git
Run Code Online (Sandbox Code Playgroud)
我该如何清除这个?谢谢.
最终编辑:
这是我最终提出的代码,您.bashrc
可以随意使用此代码在git branch
shell提示符中显示当前代码
if [ "$color_prompt" = yes ]; then
if git --version &>/dev/null; then
# PS1 Line to show current Git Branch in the Prompt
PS1='${debian_chroot:+($debian_chroot)}\[\033[01;32m\]\u@\h\[\033[00m\]:\[\033[01;34m\]\w\[\033[00m\]\[\033[01;32m\]$(__git_ps1 " (%s)")\[\033[00m\]\$ '
else
# Original PS1 Line
PS1='${debian_chroot:+($debian_chroot)}\[\033[01;32m\]\u@\h\[\033[00m\]:\[\033[01;34m\]\w\[\033[00m\]\$ '
fi
else
if git --version &>/dev/null; then
# PS1 Line to show current Git Branch in the Prompt
PS1='${debian_chroot:+($debian_chroot)}\u@\h:\w $(__git_ps1 "(%s)")\$ '
else
# Original PS1 Line
PS1='${debian_chroot:+($debian_chroot)}\u@\h:\w\$ '
fi
fi
Run Code Online (Sandbox Code Playgroud)
ayc*_*ter 21
尝试执行
git --version
Run Code Online (Sandbox Code Playgroud)
根据返回值,$?
您可以假设是否安装了git.如果你得到0一切都很好,否则没有安装git.你也可以test
这样.
假设一切都设置正确,git在$ PATH中,并且git命令未重命名.
像这样使用它
git --version 2>&1 >/dev/null # improvement by tripleee
GIT_IS_AVAILABLE=$?
# ...
if [ $GIT_IS_AVAILABLE -eq 0 ]; then #...
Run Code Online (Sandbox Code Playgroud)
#!/bin/bash
command -v git >/dev/null 2>&1 ||
{ echo >&2 "Git is not installed. Installing..";
yum install git
}
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
22302 次 |
最近记录: |