Bash

搞砸了 bash.bashrc 文件,命令不再工作

  • July 18, 2014

嘗試安裝 Java,我嘗試將其添加到可執行路徑中:

   export PATH=/usr/lib/jvm/jdk1.7.0_60/bin:$PATH

在系統範圍的/etc/bash.bashrc文件中。

由於某種原因,這不起作用,所以我使用了:

JAVA_HOME=/usr/lib/jvm/jdk1.7.0_60
PATH=$PATH:$HOME/bin:$JAVA_HOME/bin
export JAVA_HOME
export JRE_HOME
export PATH

最初這似乎工作正常,除了 now sudo,lsfind其他一切都不再工作了。

然後我嘗試擦除這些行並重新啟動系統,但該命令不可用;錯誤是:

Command 'sudo' is available in '/usr/bin/sudo'
The command could not be located because '/usr/bin' is not included in the PATH  environment variable.
sudo: command not found

為了解決這個問題,我複制了/etc/environmentin的內容/etc/bash.bashrc,添加了該行export PATH=$PATH:/usr/lib/jvm/jdk1.7.0_60/bin,然後輸入了source /etc/bash.bashrc.

同樣,一切正常,但僅在單個終端視窗上而不是在重新啟動後。

我嘗試了其他事情,目前在最後/etc/bash.bashrc有以下幾行:

export PATH=$PATH:/usr/lib/jvm/jdk1.7.0_60/bin/
export JAVA_HOME=$JAVA_HOME:/usr/lib/jvm/jdk1.7.0_60/bin/java/

但我必須在所有終端視窗中輸入source /etc/environment和才能同時獲取命令。好像我的改變不是永久的。source /etc/bash.bashrc``java

目前echo $PATH在一個清晰的終端視窗中的結果是:

/usr/lib/jvm/jdk1.7.0_60/bin/

之後source /etc/environment變成:

/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games

最終,在source /etc/bash.bashrc它之後:

/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/lib/jvm/jdk1.7.0_60/bin/

完整的 bash.bashrc 文件:

# System-wide .bashrc file for interactive bash(1) shells.

# To enable the settings / commands in this file for login shells as well,
# this file has to be sourced in /etc/profile.

# If not running interactively, don't do anything
[ -z "$PS1" ] && return

# check the window size after each command and, if necessary,
# update the values of LINES and COLUMNS.
shopt -s checkwinsize

# set variable identifying the chroot you work in (used in the prompt below)
if [ -z "$debian_chroot" ] && [ -r /etc/debian_chroot ]; then
   debian_chroot=$(cat /etc/debian_chroot)
fi

# set a fancy prompt (non-color, overwrite the one in /etc/profile)
PS1='${debian_chroot:+($debian_chroot)}\u@\h:\w\$ '

# Commented out, don't overwrite xterm -T "title" -n "icontitle" by default.
# If this is an xterm set the title to user@host:dir
#case "$TERM" in
#xterm*|rxvt*)
#    PROMPT_COMMAND='echo -ne "\033]0;${USER}@${HOSTNAME}: ${PWD}\007"'
#    ;;
#*)
#    ;;
#esac

# enable bash completion in interactive shells
#if [ -f /etc/bash_completion ] && ! shopt -oq posix; then
#    . /etc/bash_completion
#fi

# sudo hint
if [ ! -e "$HOME/.sudo_as_admin_successful" ] && [ ! -e "$HOME/.hushlogin" ] ; then
   case " $(groups) " in *\ admin\ *)
   if [ -x /usr/bin/sudo ]; then
       cat <<-EOF
       To run a command as administrator (user "root"), use "sudo <command>".
       See "man sudo_root" for details.

       EOF
  fi
  esac
fi

# if the command-not-found package is installed, use it
if [ -x /usr/lib/command-not-found -o -x /usr/share/command-not-found/command-not-found ]; then
   function command_not_found_handle {
           # check because c-n-f could've been removed in the meantime
           if [ -x /usr/lib/command-not-found ]; then
               /usr/bin/python /usr/lib/command-not-found -- "$1"
               return $?
           elif [ -x /usr/share/command-not-found/command-not-found ]; then
               /usr/bin/python /usr/share/command-not-found/command-not-found -- "$1"
              return $?
       else
          printf "%s: command not found\n" "$1" >&2
          return 127
       fi
   }
fi

export PATH=$PATH:/usr/lib/jvm/jdk1.7.0_60/bin/
export JAVA_HOME=$JAVA_HOME:/usr/lib/jvm/jdk1.7.0_60/bin/java/

任何人都可以幫助我嗎?我在 Ubuntu 12.04 LTS 下

嘗試將 bash.bashrc 恢復為預設設置,然後在 ~/.bashrc 文件中編輯 PATH 的本地副本。換句話說,把最後兩行:

export PATH=$PATH:/usr/lib/jvm/jdk1.7.0_60/bin/
export JAVA_HOME=$JAVA_HOME:/usr/lib/jvm/jdk1.7.0_60/bin/java/

在您的 ~/.bashrc 文件中而不是 /etc/bash.bashrc 中。您可能必須重新啟動才能進行更改。

如果這可行,並且您仍然希望更改是系統範圍的,那麼將 PATH 變數附加到 /etc/environment 以具有 Java 路徑。

要將您恢復/etc/bash.bashrc到原始狀態(如果您不記得那是什麼),您可以執行以下操作:

sudo rm /etc/bash.bashrc
sudo apt-get -o Dpkg::Options::="--force-confmiss" install --reinstall bash

否則@aprad046 的回答似乎是最好的解決方案。

引用自:https://unix.stackexchange.com/questions/140207