【问题标题】:how to concatenate/assemble the alias name of some bash cli commands如何连接/组装一些 bash cli 命令的别名
【发布时间】:2022-01-07 01:12:34
【问题描述】:

我在.bash_aliases 中有以下别名

ssh51ro="ssh -p12345 root@192.168.15.71" 
ssh52ro="ssh -p12345 root@192.168.8.25"
ssh53ro="ssh -p12345 root@192.168.35.33"
...
ssh59ro="ssh -p12345 root@192.168.1.9"

我想知道我是否可以在 for 循环中向多个收件人发送相同的命令。我通常会增加一个“i”变量并将别名的名称连接在一起。

for i in $( seq 1 9 ) ; do ssh5${i}ro date; done

但这似乎不起作用。它明确表示“找不到 ssh5iro 命令”。

在这种情况下如何组装和运行命令

【问题讨论】:

    标签: string bash alias


    【解决方案1】:

    不要将命令存储在变量中。使用函数。

    myssh() { ssh -p12345 root@"$@"; }
    ssh51ro() { myssh 192.168.15.71 "$@"; }
    ssh52ro() { myssh 192.168.8.25 "$@"; }
    ...
    for i in $( seq 1 9 ) ; do ssh5${i}ro date; done
    # maybe you want dynamic list of functions based on naming pattern:
    for func in $(compgen -A function | grep ssh5.ro); do "$func" date; done
    

    您可能想研究 Ansible 和 Puppet Bolt。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-04-15
      • 2010-10-19
      • 2016-01-13
      • 2015-01-04
      • 2010-12-09
      • 1970-01-01
      相关资源
      最近更新 更多