“♦: comando non trovato” in tty dopo il login


24

Ho questo problema dopo l'aggiornamento di Lubuntu dal 12.10 al 13.04.

Premo Ctrl+ Alt+ 1, inserire nome utente, la password, attendere due secondi e ottenere: ♦: command not found". Dopo questo messaggio posso digitare comandi senza problemi, ma che cos'è?

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

Il mio .bashrcfile è:

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

Il mio .profilefile è:

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

Il file /etc/profileè qui: http://paste.ubuntu.com/5781361/


1
Qual è l'output di echo $PATH? (Per favore modificalo nella tua domanda e poi rispondi).
Seth,

1
Puoi caricare i tuoi file ~ / .bashrc e ~ / .profile su paste.ubuntu.com e pubblicare i link?
Eric Carvalho,

Per riferimento futuro, la forma è chiamata "diamante".
user98085,

Aggiunto echo $PATHalla mia domanda.
Vitaly Zdanevich,

Se usi virgolette singole intorno alla variabile del tuo percorso nel tuo '.bashrc', questo problema può essere causato.
Phyatt,

Risposte:


28

Soluzione

In primo luogo, penso che ti riferisci quando vai in tty1 - Ctrl+ Alt+ F1.

Ora, penso che stia succedendo quello che hai detto molto probabilmente perché hai uno strano personaggio come ♦ ( carattere di diamante o distintivo speciale per un moderatore di askubuntu ) in ~/.bashrco ~/.profilefile o altri file che contengono vari comandi di inizializzazione.

Come puoi vedere nella prossima immagine, ho modificato il ~/.bashrcfile inserendo il carattere ♦ su una sola riga. Di conseguenza, quando apro il terminale si ottiene il problema descritto da te:

terminale

Succede lo stesso quando entro in tty1 con Ctrl+ Alt+ F1.

File contenenti inizializzazione comandi quando una shell viene invocata: /etc/profile, /etc/bashrc, ~/.bash_login, ~/.profile, ~/.bashrc, ~/.bash_aliasese forse altri. Vedi File di inizializzazione Shell .

Per verificare rapidamente se uno di questi file contiene qualcosa di sbagliato, puoi usare il sourcecomando. Per esempio:

source ~/.bashrc

profilo sorgente

Soluzione finale

Dopo aver ispezionato /etc/profileda http://paste.ubuntu.com/5781361/ , ho scoperto che sulla riga 31 è presente "Override da destra a sinistra" -‮ carattere unicode. Basta aprire il /etc/profilefile con sudo -H gedit /etc/profile, assicurarsi di eliminare questo strano personaggio e il problema scomparirà.

file di profilo

Per divertimento, ad esempio in HTML, se inserisci questo carattere unicode usando il codice decimale ( ‮) davanti a una riga, guarda cosa sta succedendo:

Questo testo è in arabo-inglese!

Un'altra soluzione più generalizzata

Troveremo il comando esatto che causa l'errore usando una " trap ".

Innanzitutto, dobbiamo creare un nuovo file di script nella ~/bindirectory, chiamiamolo lib.trap.sh( gedit ~/bin/lib.trap.sh), con all'interno:

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

Ora, l'unica cosa che devi fare è mettere la riga successiva all'inizio del file /etc/profile( sudo -H gedit /etc/profile):

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

Cambia <user_name>con il tuo nome utente. In questo modo, tutti i file che contengono comandi di inizializzazione quando viene invocata una shell passeranno attraverso la "trap".

Per verificare se, /etc/profilead esempio, è presente un comando errato , esegui nei comandi successivi del terminale:

bash source / etc / profile

Se qualcosa non va, come in questo caso, il risultato sarà:

trappola

Quindi, ora sappiamo per certo che c'è un problema ( command not found) nel /etc/profilefile alla riga 32 (non è nella riga 31 come sopra perché abbiamo inserito una nuova riga all'inizio del file).

Mille grazie a Luca Borrione per la sua sceneggiatura da questa risposta che mi ha aiutato a completare questa soluzione generalizzata.


Codice aggiunto da .bashrce .profileper domande - Non riesco a trovare ♦ qui.
Vitaly Zdanevich,

1
@VitalyZdanevich Si dovrebbe controllare tutti i file che contengono inizializzazione comandi ( .bash_aliases, .pam_environment, ecc) per somenthing strano all'interno, non necessariamente esattamente questo personaggio.
Radu Rădeanu,

2
Sì, il problema è sulla linea 31 da /etc/profile. Hai qualcosa di molto strano lì. Basta eliminare tutto tra fie e JAVA_HOME, dopo aver premuto uno o due 'Invio', tutto dovrebbe andare bene dopo. Modifica il file consudo gedit /etc/profile
Radu Rădeanu il

1
@RyanLoremIpsum No, ne sono sicuro. L'OP aveva incollato lì, non io :)
Radu Rădeanu,

1
Grande! Cancella queste due linee trasparenti in /etc/profile. Borsa strana.
Vitaly Zdanevich,

5

Per il debug degli script di inizializzazione di bash, eseguire quanto segue (dopo aver effettuato l'accesso alla console virtuale).

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

Quanto sopra viene eseguito bash in modalità interattiva ( -i) login ( -l), lo stesso del loginprogramma quando si accede a una console virtuale. -c ''lo fa uscire immediatamente dopo aver eseguito gli script di inizializzazione e -xe lo PS4=...fa emettere ogni comando, prima di eseguirli, insieme al nome file e al numero di riga di quel comando. Ciò dovrebbe aiutare a determinare quale riga di quale file risiede quel comando non valido.

In una nota a margine, ♦ è il simbolo che il carattere predefinito per la console virtuale utilizza per stampare caratteri per cui non ha un simbolo.


1

Durante la ricerca dei file di inizializzazione, può essere utile cercare l'esadecimale utilizzato per l'output di ♦. Il codice esadecimale per ♦ è 2666, secondo il carattere Unicode 'BLACK DIAMOND SUIT' . Nota: esiste almeno un altro codice esadecimale, 25C6, che produce lo stesso simbolo simile o simile. Vedi i risultati della ricerca per "diamante". Ricerca caratteri Unicode

Forse qualcosa di simile \u2666è in uno degli script. Da Bash Reference Manual for echo - "\ uhhhh il carattere Unicode (ISO / IEC 10646) il cui valore è il valore esadecimale HHHH (da una a quattro cifre esadecimali)"

Dipende dalla codifica dei caratteri utilizzata, quindi potresti voler prima cercare quelli più probabili. echo $LC_CTYPEdovrebbe restituire la codifica dei caratteri utilizzata dalla shell. Vedi Come ottenere la codifica dei caratteri del terminale


-1 perché in ~/.bash_historysono memorizzati i comandi gestiti interattivi su PS1.
Radu Rădeanu,

Grazie per la conferma. L'ho rimosso. Avrei dovuto aggiungere il resto come commento alla tua risposta, Radu?
iyrin,

Oh, non capisco - cosa devo fare? Dove nei file di inizializzazione Lubuntu? Ho provato la ricerca full-text di \u2666e ♦ in Catfish (ricerca Lubuntu) - niente. Semino history, niente. Vedo questo messaggio solo in tty solo dopo il login. Dopo echo $LC_CTYPEche ottengo una riga vuota.
Vitaly Zdanevich,

In esecuzione localedovrebbe mostrare LC_CTYPE. locale
iyrin,

Prova la risposta di Radu prima di questa! Se restringiamo il set di caratteri utilizzato nel tuo tty, puoi cercare l'occorrenza del codice carattere corrispondente per il diamante solido. Questa ricerca si rivelerebbe inutile se la ricerca RLO di Radu fosse la causa.
iyrin,

0

Scrivi il percorso completo di uno strumento noto che ti permetterà di modificare il tuo file bashrc, insieme al percorso completo del tuo file bashrc.

/bin/nano /home/username/.bashrc

Trova eventuali abusi della tua PATHvariabile e commentala. Probabilmente quando si cerca di aggiungere qualcosa al proprio percorso, è stato citato solo anziché doppio.

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

Copia il tuo .bashrc in uno strumento come https://www.shellcheck.net/ per vedere se hai problemi eclatanti con l'uso di bash.

Spero che sia d'aiuto.

Utilizzando il nostro sito, riconosci di aver letto e compreso le nostre Informativa sui cookie e Informativa sulla privacy.
Licensed under cc by-sa 3.0 with attribution required.