首页 > 解决方案 > CENTOS 7 - 安装 jdk 后 $PATH 不正确

问题描述

我是 Linux 操作系统的新手,我在 centos7 上安装了 jdk。

现在每次登录时,我都会收到以下错误消息:

/usr/libexec/grepconf.sh: line 5: grep: command not found
-bash: /home/centos/.bash_profile: line 13: syntax error near unexpected token `newline'
-bash: /home/centos/.bash_profile: line 13: `export PATH=$PATH:/opt/jdk1.8.0_192/bin:/opt/jdk1.8.0_192/jre/bin:/usr/local/sbin:/usr/sbin:/home/centos/.local/bin:/home/centos/bin:/usr/bin/grep>'

并且所有命令都“未找到”

我发现这篇文章更新路径: https ://unix.stackexchange.com/questions/302743/centos-7-all-command-not-found-except-cd

我用这个命令更新路径:

PATH=/usr/local/bin:/usr/bin:bin

所以现在使用 sudo nano /home/centos/ .bash_profile命令我在 bash_profile 文件中有以下内容:

# .bash_profile

# Get the aliases and functions
if [ -f ~/.bashrc ]; then
    . ~/.bashrc
fi

 # User specific environment and startup programs

 PATH=$PATH:$HOME/.local/bin:$HOME/bin

 export PATH
 export PATH=$PATH:/opt/jdk1.8.0_192/bin:/opt/jdk1.8.0_192/jre/bin:/usr/local/sbin:/usr/sbin:/home/centos/.local/bin:/home/centos/bin:/usr/bin/grep>

什么是问题?我该如何解决所描述的错误?

第一次编辑以回答@航海问题:

这里是 .bash_profile 文件的内容:

# .bash_profile

# Get the aliases and functions
if [ -f ~/.bashrc ]; then
        . ~/.bashrc
fi

# User specific environment and startup programs

PATH=$PATH:$HOME/.local/bin:$HOME/bin

export PATH
export PATH=$PATH:/opt/jdk1.8.0_192/bin:/opt/jdk1.8.0_192/jre/bin:/usr/local/sbin:/usr/sbin:/home/centos/.local/bin:/home/centos/bin:/usr/bin/grep

这里是 grepconf 的内容:

#!/bin/sh

case "$1" in
    -c | --interactive-color)
        ! grep -qsi "^COLOR.*none" /etc/GREP_COLORS
        ;;
    *)
        echo >&2 "Invalid / no option passed, so far only -c | --interactive-color is supported."
        exit 1
        ;;
esac

第二次编辑以回答评论中的问题:

这里 ldd /usr/bin/grep 命令输出:

linux-vdso.so.1 =>  (0x00007fff769b8000)
    libpcre.so.1 => /lib64/libpcre.so.1 (0x00007f0454df3000)
    libc.so.6 => /lib64/libc.so.6 (0x00007f0454a26000)
    libpthread.so.0 => /lib64/libpthread.so.0 (0x00007f0454809000)
    /lib64/ld-linux-x86-64.so.2 (0x000055ce09e0c000)

这里是 ls -l /lib64/ld* 的结果

[centos@serveur-1 ~]$ ls -l /lib64/ld*
-rwxr-xr-x 1 root root 163400 Jan 29 18:55 /lib64/ld-2.17.so
lrwxrwxrwx 1 root root     10 Feb 20 21:51 /lib64/ld-linux-x86-64.so.2 -> ld-2.17.so

每次我注销,然后我登录时,我总是得到“找不到命令”,除了一些命令,例如:

java -version

所以每次我必须输入“PATH=/usr/local/bin:/usr/bin:bin/”(在这个网站上找到答案)并且每次我输入 echo $PATH 时,输出都会越来越复杂:

[centos@serveur-1 ~]$ echo $PATH
$PATH:/opt/jdk1.8.0_192/bin:/opt/jdk1.8.0_192/jre/bin:/usr/local/sbin:/usr/sbin:/home/centos/.local/bin:/home/centos/bin:/opt/jdk1.8.0_192/bin:/opt/jdk1.8.0_192/jre/bin:/usr/local/sbin:/usr/sbin:/home/centos/.local/bin:/home/centos/bin:/usr/bin/grep

我想 $PATH 登录后内容很糟糕。

那么如何才能最终解决呢?

@tripleee 的第三次编辑:这里 .bashrc 文件内容:

[centos@serveur-1 ~]$ sudo nano /home/centos/.bashrc
  GNU nano 2.3.1                  File: /home/centos/.bashrc

# .bashrc

# Source global definitions
if [ -f /etc/bashrc ]; then
        . /etc/bashrc
fi

# Uncomment the following line if you don't like systemctl's auto-paging feature:
# export SYSTEMD_PAGER=

# User specific aliases and functions

它谈到/etc/bashrc,所以这是它的内容:

[centos@serveur-1 ~]$ sudo nano /etc/bashrc
  GNU nano 2.3.1                   File: /etc/bashrc

# /etc/bashrc

# System wide functions and aliases
# Environment stuff goes in /etc/profile

# It's NOT a good idea to change this file unless you know what you
# are doing. It's much better to create a custom.sh shell script in
# /etc/profile.d/ to make custom changes to your environment, as this
# will prevent the need for merging in future updates.

# are we an interactive shell?
if [ "$PS1" ]; then
  if [ -z "$PROMPT_COMMAND" ]; then
    case $TERM in
    xterm*|vte*)
      if [ -e /etc/sysconfig/bash-prompt-xterm ]; then
          PROMPT_COMMAND=/etc/sysconfig/bash-prompt-xterm
      elif [ "${VTE_VERSION:-0}" -ge 3405 ]; then
          PROMPT_COMMAND="__vte_prompt_command"
      else
          PROMPT_COMMAND='printf "\033]0;%s@%s:%s\007" "${USER}" "${HOSTNAME%%.*}" "${PWD/#$HOM$
      fi
      ;;
    screen*)
      if [ -e /etc/sysconfig/bash-prompt-screen ]; then
          PROMPT_COMMAND=/etc/sysconfig/bash-prompt-screen
      else
          PROMPT_COMMAND='printf "\033k%s@%s:%s\033\\" "${USER}" "${HOSTNAME%%.*}" "${PWD/#$HOM$
      fi
      ;;
    *)
      [ -e /etc/sysconfig/bash-prompt-default ] && PROMPT_COMMAND=/etc/sysconfig/bash-prompt-de$
      ;;
    esac
  fi
  # Turn on parallel history
  shopt -s histappend
  history -a
  # Turn on checkwinsize
  shopt -s checkwinsize
  [ "$PS1" = "\\s-\\v\\\$ " ] && PS1="[\u@\h \W]\\$ "
  # You might want to have e.g. tty in prompt (e.g. more virtual machines)
  # and console windows
  # If you want to do so, just add e.g.
  # if [ "$PS1" ]; then
  #   PS1="[\u@\h:\l \W]\\$ "
  # fi
  # to your custom modification shell script in /etc/profile.d/ directory
fi

if ! shopt -q login_shell ; then # We're not a login shell
    # Need to redefine pathmunge, it get's undefined at the end of /etc/profile
    pathmunge () {
        case ":${PATH}:" in
            *:"$1":*)
                ;;
            *)
                if [ "$2" = "after" ] ; then
                    PATH=$PATH:$1
                else
                    PATH=$1:$PATH
                fi
        esac
    }

    # By default, we want umask to get set. This sets it for non-login shell.
    # Current threshold for system reserved uid/gids is 200
    # You could check uidgid reservation validity in
    # /usr/share/doc/setup-*/uidgid file
    if [ $UID -gt 199 ] && [ "`/usr/bin/id -gn`" = "`/usr/bin/id -un`" ]; 

then
       umask 002
    else
       umask 022
    fi

    SHELL=/bin/bash
    # Only display echos from profile.d scripts if we are no login shell
    # and interactive - otherwise just process them to set envvars
    for i in /etc/profile.d/*.sh; do
        if [ -r "$i" ]; then
            if [ "$PS1" ]; then
                . "$i"
            else
                . "$i" >/dev/null
            fi
        fi
    done

    unset i
    unset -f pathmunge
fi
# vim:ts=4:sw=4

当你来自“Windows世界”时,很难理解这种机制......

标签: bashpathcentosenvironment-variables

解决方案


通过更新 .bash_profile 解决如下:

# .bash_profile

# Get the aliases and functions
if [ -f ~/.bashrc ]; then
    . ~/.bashrc
fi

# User specific environment and startup programs 


PATH=$PATH:$HOME/.local/bin:$HOME/bin:/usr/local/bin:/usr/bin:/bin:/opt/jdk1.8.0_192/bin:/opt/jdk1.8.0_192/jre/bin

export PATH

我是 Centos 的新手,所以我希望我能找回我的 2 分(被否决票删除):

  • 我已经编辑了我的问题并对其进行了结构化,以回答评论中提出的问题
  • 我一直在寻找答案(见链接),但我不明白所有的说明,所以可能很难说清楚

推荐阅读