加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 综合聚焦 > 服务器 > 安全 > 正文

如何覆盖bash命令?

发布时间:2020-12-15 21:18:02 所属栏目:安全 来源:网络整理
导读:所以我基本上试图覆盖我的ssh命令所以我只需要键入ssh,默认情况下它将连接到我的主服务器.然后,如果我传递一个参数,比如username @ server_port,那么它将运行基本命令. # Fast SSH (a working progress) TODO: make work without naming the function `fssh`
所以我基本上试图覆盖我的ssh命令所以我只需要键入ssh,默认情况下它将连接到我的主服务器.然后,如果我传递一个参数,比如username @ server_port,那么它将运行基本命令.

# 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的情况下让它工作?

所以基本上这就是正确完成时的外观:

# 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命令的绝对路径,否则它将是递归的.例如,而不是:

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

你应该写:

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

使用内置命令从PATH获取ssh(由@chepner建议):

command [-pVv] command [arg …]

06002

为什么不别名?

使用函数是正确的模式,请阅读Aliases and Functions sections from the man page.

ALIASES

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时,请执行以下操作:

>确保重新加载shell配置:source~ / .bashrc
>检查什么是ssh:哪个ssh或类型ssh

类型和哪个

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

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

声明函数ssh(){ssh my-vm; },我有:

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

建议

使用sh或bash语法:

> sh:测试是用便携但不强大的方式完成的;> bash测试完成[[…]]和函数关键字,不太便于携带,但开发人员友好.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读