“别名"带参数的命令 [英] "Aliasing" of Commands with Arguments

查看:30
本文介绍了“别名"带参数的命令的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道有些命令可以通过shell取别名,比如

I know that some commands can be aliased through the shell, such as

alias mv="cp"

但我想做类似的事情,但有参数(是的,我已经看到了 其他问题,但我的问题略有不同).类似于:

But I want to do something similar but with arguments (and yes, I have seen the other question, but my question is slightly different). Something similar to:

sshkill() {
  service sshd restart 
}
alias sshkill="killall sshd"

但这显然行不通.

我希望防止用户直接杀死 sshd(至少是意外)并使其重新启动(这是针对已发生多次的服务器).因此,除了别名,有没有办法防止命令killall sshd被执行,而是执行service sshd restart 代替?

I wish to prevent users from directly killing the sshd (at least by accident) and make it restart (this is for a server where this has happened more than once). So, rather than aliasing, is there a way to prevent the command killall sshd from being executed and rather executing service sshd restart instead?

推荐答案

你想拦截killall,所以:

killall() {
  if [ "$1" = "sshd" ]; then
    echo "some warning message, and perhaps service sshd restart"
  else
    command killall "$@"
  fi
}

这篇关于“别名"带参数的命令的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆