shell脚本的交互最常用的方式是使用菜单,通常是echo打印菜单出来。

由于服务别名都写在/etc/hosts中

192.168.155.172 test-1
192.168.155.173 test-2
192.168.155.174 test-3
192.168.155.140 test-4

 

开发人员连接后端服务器,需要从hosts中复制比较麻烦。

因此需要一个交互式脚本,简化操作。

 

二、完整代码

start.sh

#!/bin/bash
#simple script menu

#连接主机
function connect_host() {
    ssh -p 22 $1
}


function menu {
    clear
    echo
    echo -e "test menu"
    echo -e "1. test-1"
    echo -e "2. test-2"
    echo -e "3. test-3"
    echo -e "4. test-4"
    echo -e "0. Exit menu\n\n"
    #-en 选项会去掉末尾的换行符,这让菜单看起来更专业一些
    echo -en "Enter option:"
    #read 命令读取用户输入
    read -n 1 option
}

menu
case $option in
0)
    exit ;;
1)
    connect_host test-1  ;;
2)
    connect_host test-2 ;;
3)
    connect_host test-3 ;;
4)
    connect_host test-4;;
*)
    clear
    echo "sorry,wrong selection" ;;
esac

echo -en "thit any to contunue"

 

三、用户登录自动执行脚本

由于开发人员,统一使用用户:develop来进行登录。

因此,将star.sh脚本,放到路径/home/develop目录下。

修改环境脚本

vi /home/develop/.bash_profile

最后一行,增加内容:

bash ~/start.sh

 

使用develop登录,效果如下:

shell 交互脚本菜单

 

 

 

 

本文参考链接:

https://www.jianshu.com/p/091738dbab8e

相关文章:

  • 2022-12-23
  • 2021-07-15
  • 2021-06-04
  • 2021-11-05
  • 2022-01-29
  • 2021-06-10
  • 2021-05-16
猜你喜欢
  • 2022-12-23
  • 2022-12-23
  • 2021-12-14
  • 2022-12-23
  • 2022-12-23
  • 2021-12-26
  • 2021-11-07
相关资源
相似解决方案