How to alias a command with spaces? (Or a shell function) [duplicate]

强颜欢笑 提交于 2019-12-11 06:26:58

问题


All the documentation I've looked at seems to indicate that in both aliases and shell functions, the name cannot contain spaces.

What I'm trying to do is make it more difficult for other admins (as root) to run a command against our Pass implementation (doc here : https://www.passwordstore.org/). It would still be possible, but I was hoping to turn a command like "pass rm $anyValueTheyInput" and alias that to, say "echo 'You can't do that'". Of course they're admins and they can change their aliases, but it would hopefully prevent accidental removal of passwords.

Is this possible in BASH? These will all be on RHEL or Centos boxes.


回答1:


Aliasing whole commands complete with arguments is not possible in bash (if it's even possible in any UNIX shell at all).

What you can do is create a pass function that catches all the undesirable argument packs and forwards all other argument packs to command pass:

pass()
{
    if [ rm = "$1" ]; then 
        >&2 printf '%s\n' "You can't do that"
        return 1
    fi
    #more checks... ?
    #...
    #forward the sanitized argument pack into the actual pass binary/script
    command pass "$@" 
}


来源:https://stackoverflow.com/questions/45255269/how-to-alias-a-command-with-spaces-or-a-shell-function

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!