
有人可以解釋一下該exit
指令在 Unix 終端機中是如何運作的嗎?
搜索man exit
和which exit
沒有幫助,我遇到了以下問題。
在我的新 Red Hat 系統上安裝 Anaconda 和 PyCharm 的附加軟體包後,我注意到每當我調用exit
退出終端會話時,我都會收到一系列錯誤,然後終端按預期退出。這些錯誤似乎表明我對 的呼叫exit
正在觸發呼叫rm ~/anaconda3/.../
和rm ~/PyCharm/....
,從而導致錯誤。所有目錄似乎也是我為這些程式下載的軟體包的位置(即 numpy),請參見下文。
$ exit
rm: cannot remove ‘~/anaconda3/lib/python3.5/site-packages/numpy/core’: Is a directory
...
...
解決
在我的~/.bash_logout
文件中,有一行
find ~ -xdev ( -name *~ -o -name .*~ -o -name core ) -exec \rm '{}' \;
註解掉這一行可以停止錯誤訊息。它似乎搜索並刪除所有臨時文件。但它也會嘗試尋找其中包含“core”一詞的目錄,並將其刪除。這是系統預設的。
答案1
man bash
exit [n] [...] A trap on EXIT is executed before the shell terminates.
此類陷阱通常用於在退出時清理 tmpfiles,請參閱 https://stackoverflow.com/questions/687014/removing-created-temp-files-in-unexpected-bash-exit
像這樣定義一個退出陷阱(為了在新 shell 中更好地進行測試):
$ bash
$ trap "rm filetodelete" EXIT
顯示定義的 EXIT 陷阱:
$ trap -p EXIT
trap -- 'rm filetodelete' EXIT
測試:
$ exit
rm: cannot remove ‘filetodelete’: No such file or directory
請注意,exit
也可以隱式「呼叫」。因此,exit
您也可以透過 觸發陷阱kill -HUP $$
。
答案2
通常,如果您手動設定了此選項,您只會在退出 shell 時看到執行。但也許您安裝的軟體包之一附帶了 bash 退出 shell 腳本...
查看;
〜/ .bash_logout
也許你會從那裡找到一個腳本調用,這是一個奇怪的...
答案3
該exit
命令是一個特殊的命令內建shell 中的命令。它必須是內建的,因為它需要退出 shell 進程。
它退出 shell,並提供退出狀態(如果有)或最後一個命令的退出狀態。
退出時,shell 將運行EXIT
陷阱(如果有)。查看trap
目前設定的輸出(在類似 Bourne 的 shell 中)。
對於許多 shell,如果 shell 被作為登入 shell 呼叫(某些系統/使用者配置終端模擬器來啟動登入 shell),它還將運行儲存在特殊檔案中的程式碼,如~/.logout
、~/.zlogout
和~/.bash_logout
可能對應的文件,具體/etc
取決於shell 。
您可以set -x
在呼叫之前執行此操作exit
以了解這些命令是從哪裡運行的。
答案4
exit
是 bash 的「內建」指令,難怪man exit
沒有幫助。
正確的文件可以從手冊頁或使用bash ( ) 的man bash
內建指令獲得。help
help exit
$ help exit
exit: exit [n]
Exit the shell.
Exits the shell with a status of N. If N is omitted, the exit status
is that of the last command executed.
$
如果你真的想知道如何它有效,看看來源:http://git.savannah.gnu.org/cgit/bash.git/tree/builtins/exit.def?h=bash-4.4