登录后在tty中的“♦:命令未找到”


24

Lubuntu从12.10升级到13.04后,我遇到了这个问题。

我按Ctrl+ Alt+1,输入登录名,密码,等待两秒钟,并得到:♦: command not found"。出现此消息后,我可以键入命令而不会出现问题,但这是什么?

echo $PATH
/usr/lib/lightdm/lightdm:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games:/home/vitaly/bin:/usr/java/jdk1.7.0_17/bin

我的 .bashrc档案是:

# ~/.bashrc: executed by bash(1) for non-login shells.
# see /usr/share/doc/bash/examples/startup-files (in the package bash-doc)
# for examples

# If not running interactively, don't do anything
case $- in
    *i*) ;;
      *) return;;
esac

# don't put duplicate lines or lines starting with space in the history.
# See bash(1) for more options
HISTCONTROL=ignoreboth

# append to the history file, don't overwrite it
shopt -s histappend

# for setting history length see HISTSIZE and HISTFILESIZE in bash(1)
HISTSIZE=1000
HISTFILESIZE=2000

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

# If set, the pattern "**" used in a pathname expansion context will
# match all files and zero or more directories and subdirectories.
#shopt -s globstar

# make less more friendly for non-text input files, see lesspipe(1)
[ -x /usr/bin/lesspipe ] && eval "$(SHELL=/bin/sh lesspipe)"

# 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, unless we know we "want" color)
case "$TERM" in
    xterm-color) color_prompt=yes;;
esac

# uncomment for a colored prompt, if the terminal has the capability; turned
# off by default to not distract the user: the focus in a terminal window
# should be on the output of commands, not on the prompt
#force_color_prompt=yes

if [ -n "$force_color_prompt" ]; then
    if [ -x /usr/bin/tput ] && tput setaf 1 >&/dev/null; then
    # We have color support; assume it's compliant with Ecma-48
    # (ISO/IEC-6429). (Lack of such support is extremely rare, and such
    # a case would tend to support setf rather than setaf.)
    color_prompt=yes
    else
    color_prompt=
    fi
fi

if [ "$color_prompt" = yes ]; then
    PS1='${debian_chroot:+($debian_chroot)}\[\033[01;32m\]\u@\h\[\033[00m\]:\[\033[01;34m\]\w\[\033[00m\]\$ '
else
    PS1='${debian_chroot:+($debian_chroot)}\u@\h:\w\$ '
fi
unset color_prompt force_color_prompt

# If this is an xterm set the title to user@host:dir
case "$TERM" in
xterm*|rxvt*)
    PS1="\[\e]0;${debian_chroot:+($debian_chroot)}\u@\h: \w\a\]$PS1"
    ;;
*)
    ;;
esac

# enable color support of ls and also add handy aliases
if [ -x /usr/bin/dircolors ]; then
    test -r ~/.dircolors && eval "$(dircolors -b ~/.dircolors)" || eval "$(dircolors -b)"
    alias ls='ls --color=auto'
    #alias dir='dir --color=auto'
    #alias vdir='vdir --color=auto'

    alias grep='grep --color=auto'
    alias fgrep='fgrep --color=auto'
    alias egrep='egrep --color=auto'
fi

# some more ls aliases
alias ll='ls -alF'
alias la='ls -A'
alias l='ls -CF'

# Add an "alert" alias for long running commands.  Use like so:
#   sleep 10; alert
alias alert='notify-send --urgency=low -i "$([ $? = 0 ] && echo terminal || echo error)" "$(history|tail -n1|sed -e '\''s/^\s*[0-9]\+\s*//;s/[;&|]\s*alert$//'\'')"'

# Alias definitions.
# You may want to put all your additions into a separate file like
# ~/.bash_aliases, instead of adding them here directly.
# See /usr/share/doc/bash-doc/examples in the bash-doc package.

if [ -f ~/.bash_aliases ]; then
    . ~/.bash_aliases
fi

# enable programmable completion features (you don't need to enable
# this, if it's already enabled in /etc/bash.bashrc and /etc/profile
# sources /etc/bash.bashrc).
if ! shopt -oq posix; then
  if [ -f /usr/share/bash-completion/bash_completion ]; then
    . /usr/share/bash-completion/bash_completion
  elif [ -f /etc/bash_completion ]; then
    . /etc/bash_completion
  fi
fi

我的 .profile档案是:

# ~/.profile: executed by the command interpreter for login shells.
# This file is not read by bash(1), if ~/.bash_profile or ~/.bash_login
# exists.
# see /usr/share/doc/bash/examples/startup-files for examples.
# the files are located in the bash-doc package.

# the default umask is set in /etc/profile; for setting the umask
# for ssh logins, install and configure the libpam-umask package.
#umask 022

# if running bash
if [ -n "$BASH_VERSION" ]; then
    # include .bashrc if it exists
    if [ -f "$HOME/.bashrc" ]; then
    . "$HOME/.bashrc"
    fi
fi

# set PATH so it includes user's private bin if it exists
if [ -d "$HOME/bin" ] ; then
    PATH="$HOME/bin:$PATH"
fi

该文件/etc/profile位于:http : //paste.ubuntu.com/5781361/


1
输出是echo $PATH什么?(请编辑成您的问题,然后回复)。
赛斯2013年

1
您可以将〜/ .bashrc和〜/ .profile文件上传到paste.ubuntu.com并发布链接吗?
Eric Carvalho 2013年

为了将来参考,该形状称为“钻石”。
user98085 2013年

添加echo $PATH到我的问题。
Vitaly Zdanevich 2013年

如果在“ .bashrc”中的路径变量周围使用单引号,则可能导致此问题。
phyatt

Answers:


28

解决方法

首先,我认为您指的是何时进入tty1- Ctrl+ Alt+F1

现在,我认为正在发生的事情,你说的话很可能是因为你有一个奇怪的字符像♦(钻石西装字符一个askubuntu主持人的特殊徽章)中~/.bashrc~/.profile文件或包含各种初始化命令等文件。

如您在下一张图像中看到的,我编辑了~/.bashrc文件,把♦字符放在一行中。结果,当我打开终端时,会遇到您描述的问题:

终奌站

当我用Ctrl+ 进入tty1时,情况是一样的Alt + F1

包含初始化命令调用一个shell文件时:/etc/profile/etc/bashrc~/.bash_login~/.profile~/.bashrc~/.bash_aliases或者其他人。请参阅Shell初始化文件

要快速检查其中一个文件是否存在问题,可以使用source命令。例如:

source ~/.bashrc

源配置文件

最终解决方案

/etc/profilehttp://paste.ubuntu.com/5781361/进行检查后,我发现在第31行上有“从右向左重写”‮ -Unicode字符。只需使用打开/etc/profile文件sudo -H gedit /etc/profile,请确保删除该奇怪的字符,问题将消失。

个人档案

有趣的是,例如在HTML中,如果您使用十进制代码(‮在一行的前面),请查看发生了什么:

text此文本为阿拉伯语-英语!

另一个更通用的解决方案

我们将使用“ 陷阱 ”找到导致错误的确切命令 ”。

首先,我们必须在~/bin目录中创建一个新的脚本文件,我们将其命名为lib.trap.shgedit ~/bin/lib.trap.sh),其中包含以下内容:

lib_name='trap'

lib_version=20130620
#changed from lib_version=20121026 found it at /programming//a/13099228/2353900 to work well at initialization of the shell

stderr_log="/dev/shm/stderr.log"

#
# TO BE SOURCED ONLY ONCE:
#
###~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~##

if test "${g_libs[$lib_name]+_}"; then
    return 0
else
    if test ${#g_libs[@]} == 0; then
        declare -A g_libs
    fi
    g_libs[$lib_name]=$lib_version
fi


#
# MAIN CODE:
#
###~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~##

set -o pipefail  # trace ERR through pipes
set -o errtrace  # trace ERR through 'time command' and other functions
set -o nounset   ## set -u : exit the script if you try to use an uninitialised variable
set -o errexit   ## set -e : exit the script if any statement returns a non-true return value

exec 2>"$stderr_log"


###~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~##
#
# FUNCTION: EXIT_HANDLER
#
###~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~##

function exit_handler ()
{
    local error_code="$?"

    test $error_code == 0 && return;

    #
    # LOCAL VARIABLES:
    # ------------------------------------------------------------------
    #    
    local i=0
    local regex=''
    local mem=''

    local error_file=''
    local error_lineno=''
    local error_message='unknown'

    local lineno=''


    #
    # PRINT THE HEADER:
    # ------------------------------------------------------------------
    #
    # Color the output if it's an interactive terminal
    test -t 1 && tput bold; tput setf 4                                 ## red bold
    echo -e "\n(!) EXIT HANDLER\n"


    #
    # GETTING LAST ERROR OCCURRED:
    # ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ #

    #
    # Read last file from the error log
    # ------------------------------------------------------------------
    #
    if test -f "$stderr_log"
        then
            stderr=$( tail -n 1 "$stderr_log" )
            rm "$stderr_log"
    fi

    #
    # Managing the line to extract information:
    # ------------------------------------------------------------------
    #

    if test -n "$stderr"
        then        
            # Exploding stderr on :
            mem="$IFS"
            local shrunk_stderr=$( echo "$stderr" | sed 's/\: /\:/g' )
            IFS=':'
            local stderr_parts=( $shrunk_stderr )
            IFS="$mem"

            # Storing information on the error
            error_file="${stderr_parts[0]}"
            error_lineno="${stderr_parts[1]}"
            error_message=""

            for (( i = 3; i <= ${#stderr_parts[@]}; i++ ))
                do
                    error_message="$error_message "${stderr_parts[$i-1]}": "
            done

            # Removing last ':' (colon character)
            error_message="${error_message%:*}"

            # Trim
            error_message="$( echo "$error_message" | sed -e 's/^[ \t]*//' | sed -e 's/[ \t]*$//' )"
    fi

    #
    # GETTING BACKTRACE:
    # ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ #
    _backtrace=$( backtrace 2 )


    #
    # MANAGING THE OUTPUT:
    # ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ #

    local lineno=""
    regex='^([a-z]{1,}) ([0-9]{1,})$'

    if [[ $error_lineno =~ $regex ]]

        # The error line was found on the log
        # (e.g. type 'ff' without quotes wherever)
        # --------------------------------------------------------------
        then
            local row="${BASH_REMATCH[1]}"
            lineno="${BASH_REMATCH[2]}"

            echo -e "FILE:\t\t${error_file}"
            echo -e "${row^^}:\t\t${lineno}\n"

            echo -e "ERROR CODE:\t${error_code}"             
            test -t 1 && tput setf 6                                    ## white yellow
            echo -e "ERROR MESSAGE:\n$error_message"


        else
            regex="^${error_file}\$|^${error_file}\s+|\s+${error_file}\s+|\s+${error_file}\$"
            if [[ "$_backtrace" =~ $regex ]]

                # The file was found on the log but not the error line
                # (could not reproduce this case so far)
                # ------------------------------------------------------
                then
                    echo -e "FILE:\t\t$error_file"
                    echo -e "ROW:\t\tunknown\n"

                    echo -e "ERROR CODE:\t${error_code}"
                    test -t 1 && tput setf 6                            ## white yellow
                    echo -e "ERROR MESSAGE:\n${stderr}"

                # Neither the error line nor the error file was found on the log
                # (e.g. type 'cp ffd fdf' without quotes wherever)
                # ------------------------------------------------------
                else
                    #
                    # The error file is the first on backtrace list:

                    # Exploding backtrace on newlines
                    mem=$IFS
                    IFS='
                    '
                    #
                    # Substring: I keep only the carriage return
                    # (others needed only for tabbing purpose)
                    IFS=${IFS:0:1}
                    local lines=( $_backtrace )

                    IFS=$mem

                    error_file=""

                    if test -n "${lines[1]}"
                        then
                            array=( ${lines[1]} )

                            for (( i=2; i<${#array[@]}; i++ ))
                                do
                                    error_file="$error_file ${array[$i]}"
                            done

                            # Trim
                            error_file="$( echo "$error_file" | sed -e 's/^[ \t]*//' | sed -e 's/[ \t]*$//' )"
                    fi

            echo -e "ROW, FILE:\t\t${lines[2]   }\n"

                    echo -e "ERROR CODE:\t${error_code}"
                    test -t 1 && tput setf 6                            ## white yellow
                    if test -n "${stderr}"
                        then
                            echo -e "ERROR MESSAGE:\n${stderr}"
                        else
                            echo -e "ERROR MESSAGE:\n${error_message}"
                    fi
            fi
    fi

    #
    # PRINTING THE BACKTRACE:
    # ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ #

    test -t 1 && tput setf 7                                            ## white bold
    echo -e "\n$_backtrace\n"

    #
    # EXITING:
    # ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ #

    test -t 1 && tput setf 4                                            ## red bold
    echo "Exiting!"

    test -t 1 && tput sgr0 # Reset terminal

    exit "$error_code"
}
trap exit_handler ERR                                                  # ! ! ! TRAP EXIT ! ! !
#trap exit ERR                                                        # ! ! ! TRAP ERR ! ! ! 


###~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~##
#
# FUNCTION: BACKTRACE
#
###~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~##

function backtrace
{
    local _start_from_=0

    local params=( "$@" )
    if (( "${#params[@]}" >= "1" ))
        then
            _start_from_="$1"
    fi

    local i=0
    local first=false
    while caller $i > /dev/null
    do
        if test -n "$_start_from_" && (( "$i" + 1   >= "$_start_from_" ))
            then
                if test "$first" == false
                    then
                        echo "BACKTRACE IS:"
                        first=true
                fi
                caller $i
        fi
        let "i=i+1"
    done
}

return 0

现在,您唯一要做的就是将下一行放在文件/etc/profilesudo -H gedit /etc/profile)的开头:

source '/home/<user_name>/bin/lib.trap.sh'

<user_name>用您的用户名更改。这样,在调用外壳程序时,所有包含初始化命令的文件都将通过“陷阱”。

测试是否有错误的命令 /etc/profile例如,,请在terminal next命令中运行:

bash源/ etc / profile

如果出现问题(例如在这种情况下),结果将是:

陷阱

因此,现在我们可以肯定地知道command not found/etc/profile文件的第32行(不在上面的第31行,因为我们在文件的开头插入了新行)。

非常感谢Luca Borrione回答,帮助我完成了这一通用解决方案。


.bashrc.profile向问题添加了代码-我在这里找不到♦。
Vitaly Zdanevich 2013年

1
@VitalyZdanevich您应该检查所有包含初始化命令(文件.bash_aliases.pam_environment等等)的somenthing怪里面,不一定正是这种性格。
RaduRădeanu13年

2
是的,问题出在的第31行/etc/profile。你那里很奇怪。只需删除betwen fi和之间的所有内容JAVA_HOME,然后按一两个“ Enter”键,然后一切都可以了。使用sudo gedit /etc/profile
RaduRădeanu2013年

1
@RyanLoremIpsum不,我确定。OP已粘贴到那里,而不是我:)
RaduRădeanu13年

1
大!我在中清除了这两条透明线/etc/profile。奇怪的袋子。
Vitaly Zdanevich

5

要调试bash的初始化脚本,请运行以下命令(在虚拟控制台上登录后)。

PS4='+ $BASH_SOURCE:$LINENO:' bash -xlic ''

上面的代码在交互式(-i)登录(-l)模式下运行bash,与登录login虚拟控制台时的程序相同。-c ''使得它立即退出通过初始化脚本运行后,将-xPS4=...使它输出的每个命令,它运行之前,与该命令的文件名和行号。那应该有助于确定无效命令位于哪个文件的哪一行。

附带说明,♦是虚拟控制台默认字体用于打印没有符号的字符的符号。


1

搜索初始化文件时,查找用于输出♦的十六进制可能会有所帮助。根据Unicode字符“ BLACK DIAMOND SUIT”,♦的十六进制代码为2666 。注意:至少还有一个其他十六进制代码25C6,它会产生相同或相似的符号。查看“钻石”的搜索结果。Unicode字符搜索

\u2666其中一个脚本中可能包含类似内容。从Bash参考手册获取回显 -“ \ uhhhh Unicode(ISO / IEC 10646)字符,其值为十六进制值HHHH(一到四个十六进制数字)”

这取决于所使用的字符编码,因此您可能需要首先搜索最可能的字符。echo $LC_CTYPE应该返回外壳使用的字符编码。请参阅如何获取终端的字符编码


-1是因为在~/.bash_history其中存储了在PS1上交互式运行的命令。
RaduRădeanu13年

感谢您的确认。我已将其删除。Radu,我是否应该将其余内容添加为您的答案的评论?
iyrin

哦,我不明白-我需要做什么?Lubuntu中的初始化文件在哪里?我\u2666在fish鱼中尝试了全文搜索和♦(Lubuntu搜索)-什么都没有。我播种history-什么都没有。我只有登录后才能在tty中看到此消息。之后echo $LC_CTYPE,我得到空行。
Vitaly Zdanevich

运行中locale应显示LC_CTYPE。语言环境
iyrin13年

在此之前尝试Radu的答案!如果我们缩小tty中使用的字符集的范围,则可以搜索实心菱形对应字符代码的出现。如果Radu的RLO发现是原因,那么此搜索将被证明是徒劳的。
iyrin

0

写出一个已知工具的完整路径,它将使您可以编辑bashrc文件以及bashrc文件的完整路径。

/bin/nano /home/username/.bashrc

查找对您的PATH变量的任何滥用并将其注释掉。尝试在路径中添加内容时,可能会使用单引号而不是双引号。

PATH='$PATH:/path/to/new/tool' # very BAD, single quotes won't expand PATH
#    ^                       ^

PATH="$PATH:/path/to/new/tool" # Good! The double quotes allow variable expansion

将您的.bashrc复制到https://www.shellcheck.net/之类的工具中,以查看使用bash是否有任何明显的问题。

希望能有所帮助。

By using our site, you acknowledge that you have read and understand our Cookie Policy and Privacy Policy.
Licensed under cc by-sa 3.0 with attribution required.