“♦: perintah tidak ditemukan” di tty setelah login


24

Saya memiliki masalah ini setelah memutakhirkan Lubuntu dari 12,10 menjadi 13,04.

Aku tekan Ctrl+ Alt+ 1, masukkan login, password, menunggu dua detik dan mendapatkan: ♦: command not found". Setelah pesan ini saya bisa mengetik perintah tanpa masalah, tetapi apa itu?

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

.bashrcFile saya adalah:

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

.profileFile saya adalah:

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

File /etc/profileada di sini: http://paste.ubuntu.com/5781361/


1
Apa output dari echo $PATH? (Harap edit ke pertanyaan Anda dan kemudian balas).
Seth

1
Bisakah Anda mengunggah file ~ / .bashrc dan ~ / .profile ke paste.ubuntu.com dan memposting tautannya ?
Eric Carvalho

Untuk referensi di masa mendatang, bentuknya disebut "berlian".
user98085

Ditambahkan echo $PATHke pertanyaan saya.
Vitaly Zdanevich

Jika Anda menggunakan tanda kutip tunggal di sekitar variabel path Anda di '.bashrc' Anda, itu bisa menyebabkan masalah ini.
phyatt

Jawaban:


28

Penanganan masalah

Pertama, saya pikir Anda merujuk ketika Anda masuk tty1 - Ctrl + Alt+ F1.

Sekarang, saya pikir sedang terjadi apa yang Anda katakan kemungkinan besar karena Anda memiliki karakter aneh seperti ♦ ( karakter jas berlian atau lencana khusus untuk moderator askubuntu ) di ~/.bashrcatau ~/.profilefile atau file lain yang berisi berbagai perintah inisialisasi.

Seperti yang Anda lihat pada gambar berikutnya, saya diedit ~/.bashrc file yang memasukkan karakter ♦ di dalam satu baris. Akibatnya, ketika saya membuka terminal itu mendapatkan masalah yang dijelaskan oleh Anda:

terminal

Itu terjadi sama ketika saya pergi di tty1 dengan Ctrl + Alt+ F1.

File yang berisi perintah inisialisasi ketika shell dipanggil: /etc/profile, /etc/bashrc, ~/.bash_login, ~/.profile, ~/.bashrc,~/.bash_aliases dan mungkin orang lain. Lihat file inisialisasi Shell .

Untuk dengan cepat memeriksa apakah salah satu file ini memiliki sesuatu yang salah di dalamnya Anda dapat menggunakan sourceperintah. Sebagai contoh:

source ~/.bashrc

profil sumber

Solusi akhir

Setelah memeriksa /etc/profiledari http://paste.ubuntu.com/5781361/ , saya menemukan bahwa pada baris 31 ada "Override Kanan ke Kiri" -‮ karakter unicode. Cukup buka /etc/profilefile dengan sudo -H gedit /etc/profile, pastikan untuk menghapus karakter aneh ini dan masalahnya akan hilang.

file profil

Sebagai hiburan, dalam HTML misalnya, jika Anda memasukkan karakter unicode ini menggunakan kode desimal (‮ ) di depan baris, lihat apa yang terjadi:

Teks ini dalam bahasa Arab-bahasa Inggris!

Solusi lain yang lebih umum

Kami akan menemukan perintah tepat yang menyebabkan kesalahan menggunakan perangkap " ".

Pertama, kita harus membuat file skrip baru di ~/bindirektori, sebut saja lib.trap.sh( gedit ~/bin/lib.trap.sh), dengan mengikuti bagian dalam:

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

Sekarang, satu-satunya hal yang harus Anda lakukan adalah meletakkan baris berikutnya di awal file /etc/profile( sudo -H gedit /etc/profile):

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

Perubahan <user_name> dengan nama pengguna Anda. Seperti ini, semua file yang berisi perintah inisialisasi ketika shell dipanggil akan melewati "jebakan".

Untuk menguji apakah ada perintah yang salah /etc/profilemisalnya, jalankan di terminal perintah berikutnya:

sumber bash / etc / profile

Jika ada sesuatu yang salah, seperti dalam kasus ini, hasilnya adalah:

perangkap

Jadi, sekarang kita tahu pasti bahwa ada masalah ( command not found) pada /etc/profilefile di baris 32 (tidak pada baris 31 seperti di atas karena kita telah menyisipkan baris baru di awal file).

Terima kasih banyak kepada Luca Borrione atas skripnya dari jawaban ini yang membantu saya menyelesaikan solusi umum ini.


Kode tambahan dari .bashrcdan .profileke pertanyaan - Saya tidak dapat menemukan ♦ di sini.
Vitaly Zdanevich

1
@VitalyZdanevich Anda harus memeriksa semua file yang berisi inisialisasi perintah ( .bash_aliases, .pam_environment, dll) untuk somenthing dalam aneh, belum tentu persis karakter ini.
Radu Rădeanu

2
Ya, masalahnya ada di jalur 31 dari /etc/profile. Anda memiliki sesuatu yang sangat aneh di sana. Hapus saja everithing antara fidan JAVA_HOME, setelah tekan satu atau dua 'Enter' dan semuanya akan beres setelah itu. Ubah file dengansudo gedit /etc/profile
Radu Rădeanu

1
@RyanLoremIpsum Tidak, saya yakin. OP telah menempel di sana, bukan saya :)
Radu Rădeanu

1
Besar! Saya menghapus dua garis transparan ini di /etc/profile. Tas aneh.
Vitaly Zdanevich

5

Untuk debugging skrip inisialisasi bash, jalankan yang berikut (setelah login di konsol virtual).

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

Di atas berjalan bash dalam mode interaktif ( -i) login ( -l), sama seperti loginprogram ketika Anda masuk ke konsol virtual. -c ''membuatnya keluar segera setelah menjalankan skrip inisialisasi, dan -xdan PS4=...membuatnya output setiap perintah, sebelum menjalankannya, bersama dengan nama file dan nomor baris perintah itu. Itu akan membantu menentukan baris mana dari file yang perintah tidak valid berada.

Di samping catatan, ♦ adalah simbol yang digunakan font default untuk konsol virtual untuk mencetak karakter yang tidak memiliki simbol untuknya.


1

Saat mencari file inisialisasi Anda, mungkin berguna untuk mencari heksadesimal yang digunakan untuk mengeluarkan ♦. Kode hex untuk ♦ adalah 2666, menurut Karakter Unicode 'BLACK DIAMOND SUIT' . Catatan: Setidaknya ada satu kode hex lain, 25C6, yang menghasilkan simbol yang tampak sama atau mirip. Lihat hasil pencarian untuk "berlian". Pencarian Karakter Unicode

Mungkin sesuatu seperti \u2666ada di salah satu skrip. Dari Bash Reference Manual untuk echo - "\ uhhhh karakter Unicode (ISO / IEC 10646) yang nilainya adalah nilai heksadesimal HHHH (satu hingga empat digit hex)"

Tergantung pada pengkodean karakter yang digunakan, jadi Anda mungkin ingin mencari yang paling mungkin terlebih dahulu. echo $LC_CTYPEharus mengembalikan pengkodean karakter yang digunakan oleh shell Anda. Lihat Cara mendapatkan Pengkodean Karakter terminal


-1 karena dalam ~/.bash_historydisimpan perintah dijalankan interaktif di PS1.
Radu Rădeanu

Terima kasih sudah mengkonfirmasi. Saya sudah menghapusnya. Haruskah saya menambahkan sisanya sebagai komentar untuk jawaban Anda, Radu?
iyrin

Oh, saya tidak mengerti - apa yang harus saya lakukan? Di mana dalam file inisialisasi Lubuntu? Saya mencoba mencari teks lengkap untuk \u2666dan di Catfish (pencarian Lubuntu) - tidak ada. Saya menabur history- tidak ada. Saya melihat pesan ini hanya dalam tty setelah login. Setelah echo $LC_CTYPEsaya mendapat baris kosong.
Vitaly Zdanevich

Lari localeharus menunjukkan LC_CTYPE. lokal
iyrin

Coba jawaban Radu sebelum yang ini! Jika kami mempersempit kumpulan karakter yang digunakan di tty Anda, Anda dapat mencari kemunculan kode karakter yang sesuai untuk berlian padat. Pencarian ini akan terbukti sia-sia jika RLO's RLO find is tho.
iyrin

0

Tuliskan path lengkap ke alat yang dikenal yang akan memungkinkan Anda mengedit file bashrc Anda, bersama dengan path lengkap ke file bashrc Anda.

/bin/nano /home/username/.bashrc

Temukan penyalahgunaan pada PATHvariabel Anda dan beri komentar. Kemungkinan ketika mencoba menambahkan sesuatu ke jalur Anda, itu dikutip tunggal, bukan dikutip ganda.

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

Salin .bashrc Anda ke alat seperti https://www.shellcheck.net/ untuk melihat apakah Anda memiliki masalah mencolok dengan penggunaan bash.

Semoga itu bisa membantu.

Dengan menggunakan situs kami, Anda mengakui telah membaca dan memahami Kebijakan Cookie dan Kebijakan Privasi kami.
Licensed under cc by-sa 3.0 with attribution required.