![](/img/trans.png)
[英]How to get history of executed commands in screen terminal when we use screen command
[英]When using the terminal history, can I force confirmation when executing dangerous commands from my history?
我的終端工作流程涉及使用UP
, UP
, RETURN
導航bash歷史記錄。 有時我會忘記或錯誤輸入UP
的數量並調用錯誤的命令。 有時可能會有些痛苦( git clone
)或相當痛苦( git push
)。 有一天,我確定我會做非常痛苦的事情。
因此,我避免使用某些有用但潛在危險的命令。 我可以避免將這些命令添加到我的歷史記錄中 ,但是我喜歡我的歷史記錄。
我想將某些命令(或正則表達式測試)列入黑名單,因此,如果我使用歷史記錄來調用它們,則在執行之前需要進行確認:
$ git status
$ rm -rf node_modules # typed - no confirmation
$ rm -rf node_modules # UP, RETURN - confirmation
Blacklisted command - Are you sure? (yes/no)?
Shell不直接支持此功能,部分原因是命令在執行之前已存儲在歷史記錄中。 但是,通過將“危險”命令包裝在優先於實際命令的函數中,可以在很大程度上偽造它。 例如:
rm () {
local regex='[Yy]'
if history | sed '$d' | pcregrep -q "^\s+\d+\s+${FUNCNAME} $*"; then
read -p 'Are you sure? '
[[ "$REPLY" =~ $regex ]] || return 1
fi
command "$FUNCNAME" "$@"
}
秘密之處在於sed命令,它會刪除剛剛存儲在歷史記錄中的當前命令。 否則,grep將始終在歷史記錄中找到該命令。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.