sh:parse_git_branch:找不到命令

Abh*_*yan 11 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
Run Code Online (Sandbox Code Playgroud)

.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
Run Code Online (Sandbox Code Playgroud)

.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\]"
Run Code Online (Sandbox Code Playgroud)

Jen*_*y D 12

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

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

由于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
Run Code Online (Sandbox Code Playgroud)

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

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

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

  • 正确; 因为root shell几乎肯定是`/ bin/sh`,所以它不会执行任何启动文件; `sudo -i`和`su -l`将指定模拟登录,这意味着`/ etc/profile`和`.profile`将被执行. (3认同)
  • 固定.在过去的一周里,我一直在使用Linux,MacOSX和FreeBSD,我有时会混淆文件名在哪里... (2认同)
  • 它是命令行提示符的格式.在文件`.bash_prompt`的末尾,有一行以`export PS1`开头 - 这就是你定义你的提示应该包含函数`parse_git_branch`的地方. (2认同)