![如何在 Ubuntu 中教 bash 一些髒話?](https://rvso.com/image/1036633/%E5%A6%82%E4%BD%95%E5%9C%A8%20Ubuntu%20%E4%B8%AD%E6%95%99%20bash%20%E4%B8%80%E4%BA%9B%E9%AB%92%E8%A9%B1%EF%BC%9F.png)
當 bash 遇到未知命令(單字?)時,它會執行以下操作:
The program 'hello' can be found in the following packages:
* hello
* hello-debhelper
Try: sudo apt-get install <selected package>
我想知道這是如何完成的,以便我可以編輯它或在它之前添加一些內容,以交叉檢查本地詞典中的未知單詞,該詞典將包含短語:回復對,然後可以將其發送到輸出。
我很內疚沒有四處尋找足夠的資訊..但是我嘗試挖掘的一些 bash 指南沒有任何關於此的內容。也許我看錯地方了..有什麼指示嗎?
是的,我這樣做是為了每次我在程式失敗時輸入 wtf ,我都希望得到一些不錯的東西...
答案1
/etc/bash.bashrc
在您的函數定義中尋找command_not_found_handle
。
如果您想刪除該行為,請將其放入您的 .bashrc 中
[[ $(type -t command_not_found_handle) = "function" ]] &&
unset -f command_not_found_handle
如果你想定制,你可以這樣做
# see http://stackoverflow.com/questions/1203583/how-do-i-rename-a-bash-function
alias_function() {
eval "${1}() $(declare -f ${2} | sed 1d)"
}
alias_function orig_command_not_found_handle command_not_found_handle
command_not_found_handle() {
command=$1
shift
args=( "$@" )
do your stuff before
orig_command_not_found_handle "$command" "${args[@]}"
do your stuff after
}
答案2
這可能有潛在用途...
command-not-found 包給了你神奇的回應。我不確定是否可以自訂它,但可能值得一看。
我認為您想要做的事情的另一個選擇是在 .bashrc 檔案中新增一個別名,每當您鍵入「wtf」或類似的內容時,該檔案都會列印一條訊息:
alias wtf='echo "chill out man"'
將其新增至您的 ~/.bashrc 檔案中,然後執行以下操作:source $HOME/.bashrc
wtf
然後,每當您在終端機中輸入內容時,就會列印一條訊息。您也可以將此別名呼叫為列印更詳細訊息或類似內容的腳本。可能性是無止境!
答案3
此行為在系統範圍的 Bash 設定檔中定義/etc/bash.bashrc
:
# if the command-not-found package is installed, use it
if [ -x /usr/lib/command-not-found -o -x /usr/share/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 $?
elif [ -x /usr/share/command-not-found ]; then
/usr/bin/python /usr/share/command-not-found -- "$1"
return $?
else
return 127
fi
}
fi
要自訂它,只需在您自己的中重寫此函數~/.bashrc
:
function command_not_found_handle {
echo "Sorry, smotchkiss, try again."
}
答案4
@user606723,如果你想完全擺脫這種行為:
sudo apt-get remove command-not-found command-not-found-data
如果這不起作用,請嘗試以下操作:
sudo apt-get purge command-not-found command-not-found-data
如果您想恢復該行為:
sudo apt-get install command-not-found