bash 如何检查是否从 .bashrc 安装了 Git
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/7292584/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to check if Git is installed from .bashrc
提问by Sathish Manohar
I'm using Git, I've changed the following line in .bashrc
, To show the current checkedout branch in prompt, when pwd
is a Git Repo. Operating System I'm using is: Ubuntu 32bit
我正在使用 Git,我在 中更改了以下行.bashrc
,以在提示中显示当前签出的分支,何时pwd
是 Git 存储库。我使用的操作系统是:Ubuntu 32bit
# Original PS1 Line
PS1='${debian_chroot:+($debian_chroot)}\[3[01;32m\]\u@\h\[3[00m\]:\[3[01;34m\]\w\[3[00m\]$ '
I'm using this line to display current branch of git repo in shell prompt, instead of, the above line.
我正在使用这一行在 shell 提示符下显示 git repo 的当前分支,而不是上面的行。
# PS1 Line to show current Git Branch in the Prompt
PS1='${debian_chroot:+($debian_chroot)}\[3[01;32m\]\u@\h\[3[00m\]:\[3[01;34m\]\w\[3[00m\]\[3[01;32m\]$(__git_ps1 " (%s)")\[3[00m\]$ '
The Problem is when I give it to friends, Shell gives error __git_ps1: command not found
, while navigating between directories, as the script checks for git branch on changing directories.
How do I check if Git is installed and perform the branch check only if git is installed?
问题是当我把它交给朋友时,Shell__git_ps1: command not found
在目录之间导航时给出 error ,因为脚本在更改目录时检查 git branch 。如何检查是否安装了 Git 并仅在安装了 git 时才执行分支检查?
Edit:As suggested by ayckoster, I cameup with the following lines of code:
编辑:正如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)}\[3[01;32m\]\u@\h\[3[00m\]:\[3[01;34m\]\w\[3[00m\]\[3[01;32m\]$(__git_ps1 " (%s)")\[3[00m\]$ '
else
# Original PS1 Line
PS1='${debian_chroot:+($debian_chroot)}\[3[01;32m\]\u@\h\[3[00m\]:\[3[01;34m\]\w\[3[00m\]$ '
fi
else
PS1='${debian_chroot:+($debian_chroot)}\u@\h:\w$ '
fi
Now, Everytime I open the terminal I get the git --version
outputted to screen, while Git is installed, and I get the following error, while opening terminal when Git is not installed:
现在,每次打开终端时,我都会在git --version
安装 Git 时将输出显示到屏幕上,但在未安装 Git 时打开终端时出现以下错误:
The program 'git' is currently not installed. You can install it by typing:
sudo apt-get install git
How do I clear this? Thanks.
我如何清除这个?谢谢。
Final Edit:
最终编辑:
This is the code I came up with finally, Feel Free to use this code in your .bashrc
to display current git branch
in your shell prompt
这是我最后想出的代码,请随意使用此代码在您的 shell 提示中.bashrc
显示当前git branch
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)}\[3[01;32m\]\u@\h\[3[00m\]:\[3[01;34m\]\w\[3[00m\]\[3[01;32m\]$(__git_ps1 " (%s)")\[3[00m\]$ '
else
# Original PS1 Line
PS1='${debian_chroot:+($debian_chroot)}\[3[01;32m\]\u@\h\[3[00m\]:\[3[01;34m\]\w\[3[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
回答by ayckoster
Try to execute
尝试执行
git --version
Depending on the return value $?
you can assume git is installed or not. If you get 0 everything is fine otherwise git is not installed. You can also test
this.
根据返回值,$?
您可以假设是否安装了 git。如果你得到 0 一切都很好,否则 git 没有安装。你也可以test
这样。
This assumes everything is setup correctly and git is in your $PATH and the git command is not renamed.
这假设一切都正确设置并且 git 在您的 $PATH 中并且 git 命令没有重命名。
Use it like this
像这样使用它
git --version 2>&1 >/dev/null # improvement by tripleee
GIT_IS_AVAILABLE=$?
# ...
if [ $GIT_IS_AVAILABLE -eq 0 ]; then #...
回答by alex
#!/bin/bash
command -v git >/dev/null 2>&1 ||
{ echo >&2 "Git is not installed. Installing..";
yum install git
}
回答by Jonathan Liuti
using which
should help. If it returns nothing --> git is not installed.
使用which
应该有帮助。如果它什么都不返回--> git 没有安装。
回答by Mu Qiao
I recommend using hash
as it's a built-in so no need to create a new process. It also caches the command.
我建议使用,hash
因为它是内置的,因此无需创建新进程。它还缓存命令。
hash git && commands
回答by Foo Bah
use type
:
使用type
:
$ type -t git
file
$ type -t nogit
$
回答by JoelEspinal
This worked for me:
这对我有用:
GIT_VERSION="$(git --version)" if [ "$GIT_VERSION" != "command not found" ]; then echo "git already" installed else echo "git is missing" fi
GIT_VERSION="$(git --version)" if [ "$GIT_VERSION" != "command not found" ]; 然后 echo "git already" installed else echo "git is missing" fi
回答by Matthew Rankin
You could use the following to conditionally change your Bash prompt based on the existence of Git:
您可以使用以下内容根据 Git 的存在有条件地更改 Bash 提示:
PS1='$no-git-prompt'
if [ -a /usr/local/bin/git ] || [ -a /usr/local/git ]; then
PS1='$git-prompt'
The Linux Tutorial Blog entry "Tutorial: Conditions in bash scripting (if statements)"shows the different if conditions available, such as -a
to check for the existence of a file.
Linux 教程博客条目“教程:bash 脚本中的条件(if 语句)”显示了不同的 if 条件可用,例如-a
检查文件是否存在。
The problem I see with this solution is that it is dependent on the location of git
(i.e., /usr/local/bin/
or /usr/local/
in the above example). This may not be a problem since git on Ubuntu is found in /usr/local/
when git is installed via apt-get.
我在这个解决方案中看到的问题是它依赖于git
(即,/usr/local/bin/
或者/usr/local/
在上面的例子中)的位置。这可能不是问题,因为 Ubuntu 上的/usr/local/
git 是在通过 apt-get 安装 git 时找到的。
If you want to not be dependent on the installed location of git, then you could use command substitution to look at the results of the which
command.
如果你不想依赖git的安装位置,那么你可以使用命令替换来查看命令的结果which
。
PS1='$no-git-prompt'
if [ $(which git) ]; then
PS1='$git-prompt'
However, using which
has been deemed as "evil" by other answerers. (Although, I'd be interested in them fleshing out their reasoning for such statements instead of just making the declaration without providing their reasoning.)
但是,使用which
已被其他回答者视为“邪恶”。(虽然,我对他们充实他们对此类陈述的推理感兴趣,而不是仅仅发表声明而不提供他们的推理。)