繁体   English   中英

在 Ubuntu 中使用 bashrc 定义带循环的别名

[英]Using bashrc to define an alias with loop in Ubuntu

使用别名很方便,但在这种情况下

alias ssh.1='ssh user@192.168.0.1'
alias ssh.2='ssh user@192.168.0.2'
alias ssh.3='ssh user@192.168.0.3'

我想通过 ssh 连接到范围 192.169.0.[0-100],

如何使用带循环的变量来设置它

或者我只是逐行定义它?

您可以使用循环

for i in {1..100}
do
   alias ssh.$i="ssh user@192.168.0.$i"
done

您可以将以下函数添加到您的 bashrc :

ssh_range() {
    # if your first argument is a number and within the accepted range
    if [[ $1 =~ ^[0-9]+$ ]] && (( $1 >= 0 && $1 <= 100 ))
    then
        ssh "user@192.168.0.$1"
    else
        return 1
    fi
}

然后你就这样称呼它: ssh_range 1

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM