如何覆盖bash命令? [英] How to overwrite a bash command?

查看:77
本文介绍了如何覆盖bash命令?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以我基本上是想覆盖我的 ssh 命令,所以我只需要键入 ssh ,默认情况下它将连接到我的主服务器.然后,如果我给它传递了一个参数,说出 username @ server_port ,它将运行基本命令.

So I basically am trying to overwrite my ssh command so I only have to type ssh and by default it would connect to my main server. Then if I passed it an argument, say username@server_port it would then run the basic command.

# Fast SSH  (a working progress) TODO: make work without naming the function `fssh`
function fssh() {

    ALEX_SERVER_CONNECTION=$ALEX_SERVER_UNAME@$ALEX_SERVER_PORT

    # if the `ssh` argument is not set
    if [ -z "${1+xxx}" ]; then
        # echo "ALEX_SERVER_CONNECTION is not set at all";
        ssh $ALEX_SERVER_CONNECTION
    fi

    # if the `ssh` argument is set
    if [ -z "$1" ] && [ "${1+xxx}" = "xxx" ]; then
        ssh $1
    fi
}

如何在 ssh 前面没有 f 的情况下使其正常工作?

How do I get it to work without the f in front of the ssh?

基本上,这就是正确完成后的样子:

So basically this is how it looks when properly done:

# Fast SSH
function ssh() {

    ALEX_SERVER_CONNECTION=$ALEX_SERVER_UNAME@$ALEX_SERVER_PORT

    # if the `ssh` argument is not set
    if [ -z "${1+xxx}" ]; then # ssh to the default server
        command ssh $ALEX_SERVER_CONNECTION
    fi

    # if the `ssh` argument is set
    if [ -z "$1" ] && [ "${1+xxx}" = "xxx" ]; then # ssh using a different server
        command ssh $1
    fi
}

推荐答案

解决方案

您需要在函数中指定 ssh 命令的绝对路径,否则将是递归的.例如,代替:

Solution

You need to specify the absolute path to ssh command in your function otherwise it will be recursive. For instance, instead of:

function ssh() { ssh $USER@localhost; } # WRONG!

您应该写:

function ssh() { command ssh $USER@localhost; }

使用内置的 command PATH 中获取 ssh (由@chepner建议):

Use command built-in to get the ssh from the PATH (as suggested by @chepner):

命令[-pVv]命令[arg ...]

command [-pVv] command [arg ...]

  Run  command  with  args  suppressing  the normal shell function
  lookup. **Only builtin commands or commands found in the PATH  are
  executed**.

为什么不使用别名?

使用功能是正确的模式,请阅读手册页中的别名和功能"部分.

Why not Alias?

Using a function is the correct pattern, read the Aliases and Functions sections from the man page.

别名

在替换文本中没有使用参数的机制.如果参数是必需的,应该使用shell函数(请参见功能下面).

There is no mechanism for using arguments in the replacement text. If arguments are needed, a shell function should be used (see FUNCTIONS below).

诊断

命名自定义函数 ssh 时,请执行以下操作:

  1. 确保重新加载您的shell配置: source〜/.bashrc
  2. 使用以下代码检查什么是 ssh :哪个ssh type ssh

类型其中

在声明自定义函数之前,我得到了:

type and which

Prior to declaring a custom function I got:

type ssh  # → ssh is /usr/bin/ssh
which ssh # → /usr/bin/ssh

在声明 function ssh()后{ssh my-vm;} ,我得到了:

which ssh # → ssh () { ssh my-vm; }
type ssh  # → ssh is a shell function

建议

使用 sh bash 语法:

  • sh :测试是通过 […] 完成的,可移植但功能不强大;
  • bash 测试是通过 [[[…]] function 关键字完成的,其移植性较差,但对开发人员友好.
  • sh: test is done with [ … ], portable but not powerful ;
  • bash test is done [[ … ]] and function keyword, less portable but dev-friendly.

这篇关于如何覆盖bash命令?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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