bash 左侧多部分命令的别名

发布于 2024-11-14 16:17:04 字数 356 浏览 2 评论 0原文

我有一个命令(在 Linux Bash 中),我想阻止自己使用特定选项运行。但是,我仍然想使用其他选项运行该命令。因此,例如以下内容是可以的:

command opt1
command opt2

但我想禁用

command badopt

我正在考虑通过将其别名为我的配置文件中不存在的命令来执行此操作,例如

alias "command badopt"=djskagldjkgldasg

但这似乎不起作用。对于(轻松地)禁用我使用此特定选项的能力,同时保留我使用其他选项的能力,还有其他建议吗?

I have a command (in Linux Bash) that I want to prevent myself from ever running with a specific option. However, I want to still run that command with other options. So for example the following are OK:

command opt1
command opt2

But I want to disable

command badopt

I was thinking of doing this by aliasing it to a nonexistant command in my profile, like

alias "command badopt"=djskagldjkgldasg

but this doesn't seem to work. Any other suggestions for (easily) disabling my ability to use this specific option while preserving my ability to use other options?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

笑饮青盏花 2024-11-21 16:17:04
$ cat >> $HOME/.bashrc
shutdown () {
  if [ "x$1" = x-h ]; then
    echo Please do not run shutdown with the -h option.
    return
  fi
  /sbin/shutdown "$@"
}

# 已更新

$ 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

爱的十字路口 2024-11-21 16:17:04

bash 函数就是你想要的。假设你要拦截的命令名为“foo”:

foo () {
    case "$1" in 
        badopt) 
            echo "do not push this button again" >&2
            return 1
            ;;
    esac
    command foo "$@"
}

关键字command用于防止函数递归调用自身。

bash functions are what you want. Assuming the command you want to intercept is named "foo":

foo () {
    case "$1" in 
        badopt) 
            echo "do not push this button again" >&2
            return 1
            ;;
    esac
    command foo "$@"
}

The keyword command is used to prevent the function from calling itself recursively.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文