Hello,
I was wondering if they was a way to prevent some commands from being executed in order to prevent from bad manipulation sometimes (for exemple you execute "rm *.py" when you wanted to execute "rm *.pyc" or something like that).
People will say that it's the user's responsability to check his inputs and it's right but I would like to know anyway if there's a way.
For "basic" things, we can use aliases in our bashrc like :
alias apt-get="echo 'We use aptitude here !'"
alias sl="echo 'Did you mean ls ?'"
But for something with arguments like "rm -f *.py" (or "rm -Rf /"), this simple trick don't work. Of course, I just want a basic method that prevent the exact command to be executed (same spaces and same arguments ordering would be a good start).
Thank you very much for your answers.
EDIT :
Here's a short python script based on the idea of simply wrapping command like "rm". Bash wrapper are maybe a better idea but I like 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();