Bash 提示符丢失,现在在 PuTTY 中显示错误

Bash 提示符丢失,现在在 PuTTY 中显示错误

我的 VPS 上的终端在 PuTTY 中最初如下所示:

[root@user ~]#

那么现在看起来是这样的:

-bash-4.1#

这是现在出现的错误:

-bash: /root/.bash_profile: line 6: syntax error near unexpected token `fi'
-bash: /root/.bash_profile: line 6: `fi'

我不知道发生了什么事导致了这种情况。

我怎样才能恢复预期[root@user ~]#

里面看起来/root/.bash_profile像这样:

PuTTY 对“/root/.bash_profile”内容的截图

答案1

问题的原因和解决方法是两码事。我所知道的是,这些是相当标准的 Bash.bash_profile文件的内容;来自 RedHat 7:

# .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

这些是相当标准的 Bash 文件的内容.bashrc;通过 RedHat 7:

# .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

另外,RedHat 7上的内容/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/#$HOME/~}"'
      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/#$HOME/~}"'
      fi
      ;;
    *)
      [ -e /etc/sysconfig/bash-prompt-default ] && PROMPT_COMMAND=/etc/sysconfig/bash-prompt-default
      ;;
    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

还请注意,您只能使用纯文本编辑器来编辑此类文件。如果您使用文字处理器或任何比它更“花哨”的程序,您的文件可能会添加额外的垃圾和“小精灵”,这会导致系统卡住。

答案2

删除~/.bash_profile并添加下面的内容~/.profile
也适用于其他 shell。

# ~/.profile: executed by Bourne-compatible login shells.

if [ "$BASH" ]; then
  if [ -f ~/.bashrc ]; then
    . ~/.bashrc
  fi
fi

export PATH="${PATH}:${HOME}/bin"

一定要记住,不同的 shell 如何处理启动文件。
下面是图表

如果您使用任何 Windows 程序在基于 Unix 的计算机上编辑脚本文件或配置文件的内容,则应始终记住这两个操作系统中的行尾是不同的。在 Linux 上,行尾是字符0x0A。要么使用支持在 Unix 和 Windows 行尾之间切换的编辑器,例如,Notepad++要么使用移植到 Windows 的vim编辑器。

答案3

正如 Alex 提到的,您可能无意中创建了行尾问题。假设您已安装该实用程序,请尝试:

dos2unix /root/.bash_profile

这将转换新行字符。

相关内容