sh: parse_git_branch: 命令未找到。

20
我在OSX El Captain上启用了root权限。我已经尝试了stackoverflowsupersu上提供的一些解决方案,但无法解决错误。我从.bash_prompt导出function parse_git_branch().bash_profile中,但仍然出现此错误。我不懂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个回答

阿里云服务器只需要99元/年,新老用户同享,点击查看详情
18

问题出在当你使用sudo su时,你虽然切换到了root用户,但你仍然保留着自己的配置文件。这个配置文件包含了对命令提示符的设置,其中引用了一个bash函数。但是当你sudo到root用户时,你实际上得到的是root的shell,而不是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是不同的东西。)

1
一样的。我会编辑我的回答,消除混淆。 - Jenny D
2
已解决。在过去的一周中,我一直在使用Linux、MacOSX和FreeBSD工作,有时候会混淆哪个文件名应该放在哪里... - Jenny D
2
这是命令行提示符的格式。在你的文件.bash_prompt的末尾,有一行以export PS1开头的代码 - 这就是你定义提示符应包含函数parse_git_branch的地方。 - Jenny D
.bashrc.bash_profile不是相同的东西,但在Mac OS X上,终端窗口通常会打开一个登录shell(使用.bash_profile),而不是常规交互式shell(使用.bashrc)。标准做法是将诸如PS1之类的内容放在.bashrc中,但不要导出它。由于它没有被导出,所以它不会出现在root shell的环境中,从而解决了问题。 - chepner
3
正确;由于根 shell 几乎肯定是 /bin/sh,它不会执行任何启动文件;sudo -isu -l 命令会指定模拟登录,这意味着 /etc/profile.profile 文件会被执行。 - chepner
显示剩余5条评论

1

你是否导出了 $PS1?您可以通过运行以下命令进行检查:

printenv
否则,您应该通过运行以下代码来导出它:
export -n PS1
在此之后,您将能够无问题地运行sudo或sudo su。

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,