我想禁止使用rm
,除了在某些情況下。我在.sh
文件中編寫了一個名爲remove
的函數,它會在實際調用rm
之前通過某些檢查來執行。但是,仍然可以進入終端,只需調用rm
函數而不是使用remove
。有沒有辦法來禁用rm
函數,除非被remove
調用?我希望它能像rm
函數對登錄到終端的用戶不存在「存在」,所有「存在」都是刪除功能。禁用終端命令
甚至可能會更進一步,當用戶呼叫rm
時,它會在屏幕上打印一條聲明使用remove
的聲明。
作爲一個更廣泛的問題,有沒有辦法禁用終端命令,除了在某些情況下?我知道我只需要爲rm
製作一個別名即可,但這是簡單且不太方便的解決方法。
#!/bin/bash
function rm {
if [ $# -le 0 ]; then
echo "Error: no arguments specified."
else
hasDir=0
for arg in "[email protected]"; do
if [ -d "$arg" ]; then hasDir=1; fi
done
ac="Action canceled."
resp=("y" "n" "e")
sure=" "
while [ "$sure" != "y" ] && [ "$sure" != "n" ]; do
read -p "PERMANENT ACTION. Are you sure? (y/n): " sure
done
if [ "$sure" == "n" ]; then echo "$ac"; return; fi
if [ $hasDir -eq 1 ]; then
direc=" "
validResp=0
while [ $validResp -eq 0 ]; do
read -p "Remove all sub-directories? (y/n/e): " direc
for ans in "${resp[@]}"; do
if [ "$direc" == "$ans" ]; then validResp=1; fi
done
done
if [ "$direc" == "e" ]; then echo "$ac"; return; fi
else
direc="n"
fi
check=" "
validResp=0
while [ $validResp -eq 0 ]; do
read -p "Verify removal of each file? (y/n): " check
for ans in "${resp[@]}"; do
if [ "$check" == "$ans" ]; then validResp=1; fi
done
done
if [ "$check" == "e" ]; then echo "$ac"; return; fi
if [ "$direc" == "n" ]; then
if [ "$check" == "n" ]; then
for file in "[email protected]"; do
if [ ! -d "$file" ]; then command rm -f "$file"; fi
done
else
for file in "[email protected]"; do
if [ ! -d "$file" ]; then command rm -i "$file"; fi
done
fi
else
if [ "$check" == "n" ]; then
command rm -rf "[email protected]"
else
command rm -ir "[email protected]"
fi
fi
fi
}
我擔心所有那些你不知道使用''rm''的腳本 - 從''cron''和''init *'''等等。你能不能改變PATH來讓你的''rm''首先它不會阻止人們執行''/ bin/rm''雖然。 – sotapme 2013-02-15 23:58:07
'rm'是一個可執行文件,而不是一個shell命令或函數。請記住除了'bash'之外還有其他的shell。例如,運行'zsh'將會繞過您嘗試創建的任何基於shell的障礙。 – chepner 2013-02-16 00:12:07
問題是我無法命名我的函數'rm',因爲它進入遞歸狀態。這就是爲什麼我必須將其命名爲其他東西,特別是「刪除」。 – nick 2013-02-16 00:14:44