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


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM