Closed. This question is off-topic. It is not currently accepting answers. Learn more。
想改进这个问题吗?Update the question所以堆栈溢出的值小于aa>。
我想禁用rm
的使用,除非在某些情况下。我在remove
文件中编写了一个名为.sh
的函数,在实际调用rm
之前,它会经过我想要强制执行的某些检查。然而,人们仍然可以进入终端,简单地调用rm
函数,而不是使用remove
。是否有方法禁用rm
函数,除非由remove
调用?我要让它看起来像“cccc”函数不存在于登录到终端的用户中,所有“存在”都是删除函数。
甚至可以更进一步,当用户调用rm
时,它会在屏幕上打印一条声明,说要使用rm
。
作为一个更广泛的问题,除了在某些情况下,是否有办法禁用终端命令?我知道我可以为remove
创建一个需要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
}
最佳答案
您可以覆盖rm
(或任何其他命令);内置的command
允许您在必要时访问原始命令。
rm () {
# Do something in addition to removing the file
command rm "$@"
}
command
禁用shell函数查找。关于linux - 禁用终端命令,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/14905298/
10-14 03:23