<分区>
除某些情况外,我想禁用 rm
的使用。我在 .sh
文件中编写了一个名为 remove
的函数,它通过了我想在实际调用 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
}