我想rm
在某些情况下禁用 except 。我写了一个remove
在.sh
文件中调用的函数,它通过了我想在实际调用之前强加的某些检查rm
。但是,仍然可以进入终端并简单地调用rm
函数而不是使用remove
. 有没有办法禁用该rm
功能,除非被调用remove
?我希望它对登录终端的用户来说好像该rm
功能不“存在”,所有“存在”的是删除功能。
甚至可能更进一步,当用户调用rm
它时,它会在屏幕上打印一条声明说使用remove
.
作为一个更广泛的问题,除了在某些情况下,有没有办法禁用终端命令?我知道我可以为rm
需要 root 设置一个别名,但这是一种简单且不太方便的方法。
#!/bin/bash
function rm {
if [ $# -le 0 ]; then
echo "Error: no arguments specified."
else
hasDir=0
for arg in "$@"; 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 "$@"; do
if [ ! -d "$file" ]; then command rm -f "$file"; fi
done
else
for file in "$@"; do
if [ ! -d "$file" ]; then command rm -i "$file"; fi
done
fi
else
if [ "$check" == "n" ]; then
command rm -rf "$@"
else
command rm -ir "$@"
fi
fi
fi
}