sh:parse_git_branch:找不到命令

时间:2015-11-23 06:29:00

标签: git macos bash su

我在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\]"

2 个答案:

答案 0 :(得分:11)

这里的问题是,当您执行sudo su时,您将更改为root,但您保留自己的个人资料。该配置文件包含引用bash函数的命令提示符的设置。但是当你suto root时,你得到的是root的shell,sh而不是bash - 所以任何依赖bash配置的修改都行不通,包括你在{{{{{{ 1}}。

所以,首先要做的是确保你在sudo时实际上运行bash而不是sh。这非常简单 - 您只需运行PS1,而不是运行sudo su

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

如果您仍然遇到问题,可能是因为您的.bash_profile包含对当前用户主目录的引用,因为它指向这些行中的sudo bash

~

当您自己运行bash时,for file in ~/.{path,bash_prompt,exports,aliases,functions,extra}; do [ -r "$file" ] && source "$file" done 将扩展到您自己的主目录 - 但是当您以root用户身份运行它时,它将评估为~,这将是您查找文件的位置。

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

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

答案 1 :(得分:0)

你输出$ PS1了吗? 您可以通过运行命令检查:

printenv

否则你应该通过run:

导出它
export -n PS1

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