我有一个命令(在Linux Bash中),我想阻止自己运行特定选项。但是,我仍然想用其他选项运行该命令。例如,以下是可以的:
command opt1
command opt2
但我想禁用
command badopt
我想通过将其别名化到我的个人资料中的非抗性命令来做到这一点,比如
alias "command badopt"=djskagldjkgldasg
但这似乎不起作用。是否有其他建议(轻松)禁用我使用此特定选项的能力,同时保留我使用其他选项的能力?
答案 0 :(得分:3)
$ cat >> $HOME/.bashrc
shutdown () {
if [ "x$1" = x-h ]; then
echo Please do not run shutdown with the -h option.
return
fi
/sbin/shutdown "$@"
}
#updated
答案 1 :(得分:0)
bash功能是你想要的。假设您要拦截的命令名为“foo”:
foo () {
case "$1" in
badopt)
echo "do not push this button again" >&2
return 1
;;
esac
command foo "$@"
}
关键字command
用于阻止函数递归调用自身。