(对于为例,你执行“RM *。PY”当你想要执行“RM *。PYC”或类似的东西),我想知道,如果他们是为了防止为了从坏的操作,以防止有时正在执行一些命令的方式。
人们会说,这是用户的责任,以检查他的投入和它是正确的,但我想反正知道如果有一种方式。
对于“基本”的事情,我们可以在我们的.bashrc像使用别名:
alias apt-get="echo 'We use aptitude here !'"
alias sl="echo 'Did you mean ls ?'"
但对于一些有争论,如“RM -f *的.py”(或“室射频/”),这简单的一招不起作用。 当然,我只是想执行防止确切的命令的基本方法(同样的空间,同样的参数排序将是一个良好的开端)。
非常感谢你对你的答案。
那么,你可以用把自己的路组件之一中的所有其他人面前的由来已久的做法:
PATH=~/safebin:$PATH
然后,在~/safebin
,你把那些“更安全”之类的脚本rm
:
#!/bin/bash
for fspec in "$@" ; do
if [[ "${fspec: -3}" = ".py" ]] ; then
echo Not removing ${fspec}, use /bin/rm if you really want to.
else
echo Would /bin/rm "${fspec}" but for paranoia.
fi
done
该脚本输出rm *
:
Would /bin/rm chk.sh but for paranoia.
Would /bin/rm go but for paranoia.
Would /bin/rm go.sh but for paranoia.
Would /bin/rm images but for paranoia.
Would /bin/rm images_renamed but for paranoia.
Would /bin/rm infile.txt but for paranoia.
Would /bin/rm jonesforth.S but for paranoia.
Would /bin/rm jonesforth.f but for paranoia.
Would /bin/rm mycode.f but for paranoia.
Would /bin/rm num1.txt but for paranoia.
Would /bin/rm num2 but for paranoia.
Would /bin/rm num2.txt but for paranoia.
Would /bin/rm proc.pl but for paranoia.
Would /bin/rm qq but for paranoia.
Would /bin/rm qq.c but for paranoia.
Would /bin/rm qq.cpp but for paranoia.
Would /bin/rm qq.in but for paranoia.
Not removing qq.py, use /bin/rm if you really want to.
Would /bin/rm qq.rb but for paranoia.
Would /bin/rm qq.s but for paranoia.
Would /bin/rm qq1 but for paranoia.
Would /bin/rm qq2 but for paranoia.
Would /bin/rm qqq but for paranoia.
Would /bin/rm rm but for paranoia.
Would /bin/rm source.f90 but for paranoia.
Would /bin/rm test.txt but for paranoia.
Would /bin/rm xx but for paranoia.
Not removing xx.py, use /bin/rm if you really want to.
现在很明显的"${fspec: -3}" = ".py"
是一种简单的一个黑名单。 我可能会更喜欢有东西,我被允许删除,并否认一切白名单。
下面是基于正则表达式的白名单版本:
#!/bin/bash
for fspec in "$@" ; do
del=0
if [[ ! -z "$(echo "${fspec}" | grep 'a.e')" ]] ; then
del=1
fi
if [[ ! -z "$(echo "${fspec}" | grep '\.[Ss]$')" ]] ; then
del=1
fi
if [[ ${del} -ne 1 ]] ; then
echo "Not removing ${fspec}, use /bin/rm if you want."
else
echo " Removing ${fspec}"
#/bin/rm "${fspec}
fi
done
其输出:
Not removing chk.sh, use /bin/rm if you want.
Not removing go, use /bin/rm if you want.
Not removing go.sh, use /bin/rm if you want.
Removing images
Removing images_renamed
Not removing infile.txt, use /bin/rm if you want.
Removing jonesforth.S
Not removing jonesforth.f, use /bin/rm if you want.
Not removing mycode.f, use /bin/rm if you want.
Not removing num1.txt, use /bin/rm if you want.
Not removing num2, use /bin/rm if you want.
Not removing num2.txt, use /bin/rm if you want.
Not removing proc.pl, use /bin/rm if you want.
Not removing qq, use /bin/rm if you want.
Not removing qq.c, use /bin/rm if you want.
Not removing qq.cpp, use /bin/rm if you want.
Not removing qq.in, use /bin/rm if you want.
Not removing qq.py, use /bin/rm if you want.
Not removing qq.rb, use /bin/rm if you want.
Removing qq.s
Not removing qq1, use /bin/rm if you want.
Not removing qq2, use /bin/rm if you want.
Not removing qqq, use /bin/rm if you want.
Not removing rm, use /bin/rm if you want.
Not removing source.f90, use /bin/rm if you want.
Not removing test.txt, use /bin/rm if you want.
Not removing xx, use /bin/rm if you want.
Not removing xx.py, use /bin/rm if you want.
你可能想看看这个http://code.google.com/p/safe-rm/
你还可以使用鲜为人知命令“命令”(这是类似于“内置”命令)来创建限制性RM包装:
help builtin command | less
# test example using ls instead of rm
function ls() {
for ((i=1; i<=$#; i++ )); do
arg="${@:i:1}"
echo "arg ${i}: ${arg}"
[[ "${arg}" == \*.py ]] && { echo "Not allowed: ${arg}"; return 1; }
done
command ls "${@}"
return 0
}
ls -a
ls -a *.py
更正我的LS功能例如:
[[ "${arg}" != \*.py ]] && [[ "${arg}" == *.py ]] && \
{ echo "Not allowed: ${arg}"; return 1; }
然后:
ls -a *.py
# ... and also play with modified shell wildcard expansion behaviour ...
( shopt -s nullglob; ls -a *.py )
ThR37说:
这里的基础上,简单地包装指令,如“RM”的理念很短的Python脚本。 击包装也许是一个更好的主意,但我喜欢蟒蛇:):
#!/usr/bin/python
# coding=UTF-8
import getopt, sys
import subprocess
import re
exprs=[]
exprs.append(re.compile(".*\.py$"));
exprs.append(re.compile(".*\.cpp$"));
exprs.append(re.compile(".*\.hpp$"));
exprs.append(re.compile("\*$"));
def main():
try:
opts, args = getopt.getopt(sys.argv[1:], "Rrfiv", ["interactive","no-preserve-root","preserve-root","recursive","verbose","help","version"])
except getopt.GetoptError, err:
# print help information and exit:
print str(err)
usage()
optsString = "".join([opt[0]+opt[1]+" " for opt in opts]);
for arg in args:
tab = [expr.match(arg) for expr in exprs];
if tab.count(None)!=len(tab):
print "Not removing "+str(arg);
else
cmd = ["/bin/rm"]+[opt[i] for opt in opts for i in range(2) if not opt[i]=='']+[str(arg)];
rmfile = subprocess.Popen(cmd)
if __name__=="__main__":
main();