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

问题描述

我知道某些命令可以通过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(至少是无意间)并使它重新启动(这是针对已多次发生的服务器)。因此,除了 aliasing 之外,还有一种方法可以防止阻止命令 killall sshd 被执行,而是执行 service sshd重新启动

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
}

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

10-15 03:03