shell 交互脚本菜单

一、概述

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登录,效果如下:

 

 

 

 

本文参考链接:

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

posted @ 2020-04-04 18:01  肖祥  阅读(1531)  评论(0编辑  收藏  举报