在 bash 中輸入無效命令時如何修復“/usr/bin/python:沒有這樣的檔案或目錄”?

在 bash 中輸入無效命令時如何修復“/usr/bin/python:沒有這樣的檔案或目錄”?

我剛剛在 Windows 上的 Windows Subsystem For Linux (WSL) 下安裝了 Ubuntu 18.04.4 LTS。當我運行 bash 終端機中不存在的命令時(例如,如果我不小心添加了額外的 s ls),我收到此錯誤:

$ lss
-bash: /usr/bin/python: No such file or directory

錯誤是對的,我只安裝了python3:

$ ls /usr/bin/python*
/usr/bin/python3           /usr/bin/python3-jsonpatch    /usr/bin/python3-jsonschema  /usr/bin/python3.6m
/usr/bin/python3-jsondiff  /usr/bin/python3-jsonpointer  /usr/bin/python3.6           /usr/bin/python3m

從以前使用 Ubuntu 的過程中,我記得該訊息應該是「程式『lss』可以在以下軟體包中找到:」我不再需要該訊息,我不想只是為了讓它工作而安裝 python2 (這個問題有安裝 py2 解決問題的答案)。

是否可以在不安裝 python2、將 python 3 符號鏈接為 2 或變得完美並且不再出現拼寫錯誤的情況下解決此問題?

理想情況下,我可以將“命令不存在”腳本移植到 python3 或完全禁用它

答案1

Python3 並不是 python2 的替代品。您需要 python3 版本的lss.

lss如果我是你我就會更新。它是一個獨立的腳本,原始碼可以在github。 1 顯而易見:

print 'Usage:', __file__, '/path/to/dir'

使用python3時,「print」需要使用「(」和「)」。我會將腳本放入名為lss3或類似名稱的檔案中,並用於python3 lss3尋找和修復錯誤。

答案2

事實證明我重寫了該command_not_found_handle函數(bash 功能python)以及直接在我的 bashrc 中調用的東西:

# if the command-not-found package is installed, use it
if [ -x /usr/lib/command-not-found ]; then
    function command_not_found_handle {
        # check because c-n-f could've been removed in the meantime
        if [ -x /usr/lib/command-not-found ]; then
           /usr/bin/python /usr/lib/command-not-found -- $1
           return $?
        else
           return 127
        fi
    }
fi

這段程式碼不再需要(曾經是嗎?),刪除它解決了我的問題——現在舊的「可以在以下包中找到」輸出再次出現。

為了完整性:如果我想跳過 python 程式碼和命令不存在的簡單輸出,我可以將其放入我的 bashrc 中:

function command_not_found_handle {
    echo Command not found: $1
    return 127
}

相關內容