繁体   English   中英

sh:parse_git_branch:找不到命令

[英]sh: parse_git_branch: command not found

我在osx El Captain上启用了root。 我尝试了一些已经在stackoverflowsupersu上提供的解决方案但无法修复错误。 我导出function parse_git_branch().bash_profile.bash_prompt但我仍然得到这个错误。 我不知道bash脚本,所以我不知道发生了什么,需要修复什么。

abhimanyuaryan at Macbook in ~
$ sudo su
sh: parse_git_branch: command not found
root at Macbook in /Users/abhimanyuaryan

.bash_profile中

if which rbenv > /dev/null; then eval "$(rbenv init -)"; fi

# Add Homebrew `/usr/local/bin` and User `~/bin` to the `$PATH`
PATH=/usr/local/bin:$PATH
PATH=$HOME/bin:$PATH
export PATH

# Load the shell dotfiles, and then some:
# * ~/.path can be used to extend `$PATH`.
# * ~/.extra can be used for other settings you don’t want to commit.
for file in ~/.{path,bash_prompt,exports,aliases,functions,extra}; do
  [ -r "$file" ] && source "$file"
done
unset file

.bash_prompt

# @gf3’s Sexy Bash Prompt, inspired by “Extravagant Zsh Prompt”
# Shamelessly copied from https://github.com/gf3/dotfiles
# Screenshot: http://i.imgur.com/s0Blh.png

if [[ $COLORTERM = gnome-* && $TERM = xterm ]] && infocmp gnome-256color >/dev/null 2>&1; then
  export TERM=gnome-256color
elif infocmp xterm-256color >/dev/null 2>&1; then
  export TERM=xterm-256color
fi

if tput setaf 1 &> /dev/null; then
  tput sgr0
  if [[ $(tput colors) -ge 256 ]] 2>/dev/null; then
    # Changed these colors to fit Solarized theme
    MAGENTA=$(tput setaf 125)
    ORANGE=$(tput setaf 166)
    GREEN=$(tput setaf 64)
    PURPLE=$(tput setaf 61)
    WHITE=$(tput setaf 244)
  else
    MAGENTA=$(tput setaf 5)
    ORANGE=$(tput setaf 4)
    GREEN=$(tput setaf 2)
    PURPLE=$(tput setaf 1)
    WHITE=$(tput setaf 7)
  fi
  BOLD=$(tput bold)
  RESET=$(tput sgr0)
else
  MAGENTA="\033[1;31m"
  ORANGE="\033[1;33m"
  GREEN="\033[1;32m"
  PURPLE="\033[1;35m"
  WHITE="\033[1;37m"
  BOLD=""
  RESET="\033[m"
fi

export MAGENTA
export ORANGE
export GREEN
export PURPLE
export WHITE
export BOLD
export RESET

function parse_git_dirty() {
  [[ $(git status 2> /dev/null | tail -n1) != *"working directory clean"* ]] && echo "*"
}

function parse_git_branch() {
  git branch --no-color 2> /dev/null | sed -e '/^[^*]/d' -e "s/* \(.*\)/\1$(parse_git_dirty)/"
}

export PS1="\[${BOLD}${MAGENTA}\]\u \[$WHITE\]at \[$ORANGE\]\h \[$WHITE\]in \[$GREEN\]\w\[$WHITE\]\$([[ -n \$(git branch 2> /dev/null) ]] && echo \" on \")\[$PURPLE\]\$(parse_git_branch)\[$WHITE\]\n\$ \[$RESET\]"
export PS2="\[$ORANGE\]→ \[$RESET\]"

这里的问题是,当你做sudo su ,你正在改为root,但你保留自己的个人资料。 该配置文件包含引用bash函数的命令提示符的设置。 但是,当你sudo来根,你得到root的外壳,这是sh ,而不是bash -这样依靠bash的配置中的任何修改将无法正常工作,包括你在你所引用的功能PS1

因此,要做的第一件事就是确保在sudo时实际运行bash而不是sh。 这很简单 - 你只需运行sudo bash ,而不是运行sudo su

由于sudo默认切换到root,因此您现在将以root身份运行bash shell,而不是仅切换到root用户的默认shell。

如果你还有问题,这可能是由于包含到当前用户的主目录的引用你的.bash_profile,在它指向~在这几行:

for file in ~/.{path,bash_prompt,exports,aliases,functions,extra}; do
  [ -r "$file" ] && source "$file"
done

当您自己运行bash时, ~将扩展到您自己的主目录 - 但是当您以root身份运行它时,它将评估为/var/root并且它将在哪里查找您的文件。

有三种方法可以解决这个问题; 选择你喜欢的任何一个。

  1. 更改.bash_profile,使其包含主目录的完整路径,而不仅仅是代字号
  2. 将所有相关的bash文件复制到/var/root
  3. 不要运行sudo su ,而是sudo su - 这将为您提供root环境,而不是您自己的环境。 缺点是您无法使用所有自己的环境修改,并且您将运行sh而不是bash (在某些操作系统中,这些是相同的,但在其他操作系统中则不是。我相信MacOSX是shbash不同的东西之一。)

你有出口你的$ PS1? 您可以通过运行命令检查:

printenv

否则你应该通过运行导出它:

export -n PS1

之后你可以毫无问题地运行sudo或sudo su

暂无
暂无

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

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