zoukankan      html  css  js  c++  java
  • 【1.4】shell基本实践——根据文件做交互选择

    #!/bin/sh
    function createFile()
    {
            for f in "$@"; do
                    if [ ! -f $f ]; then
                            touch $f
                            chmod 777 $f
                    fi
            done
    }
    
    
    
    function init()
    {
            dir=`pwd`
            mkdir -p ${dir}/data/iplist/
            #ipFileChina='/data/iplist/sq_iplist_china.txt'
            #ipFileAbroad='/data/iplist/sq_iplist_abroad.txt'
            #ipFileAbroadTest='/data/iplist/sq_iplist_abroad_test.txt'
           # ipFileAbroadKuafu='/data/iplist/sq_iplist_abroad_kuafu.txt'
           # ipFileAbroadOrder='/data/iplist/sq_iplist_abroad_order.txt'
            
            ipListFile="${dir}/data/iplist/iplist.txt"
            createFile ${ipListFile}
            #source /data/gameTools/config/toolsConfig.sh
            #----------------------------------------
            #mysql账户密码
            slaveUser='a'
            slavePwd='b'
            sshPwd='c'
    }
    
    function checkPwd()
    {
            pwdCount=1
            read -s -p "Please input the password: " thePwd
            clear
            while true; do
                    if [ "${pwdCount}" -ge 3 ]; then
                            echo "bye"
                            exit 1
                    fi
                    if [ "$thePwd" = "7roaddba" ]; then
                            break
                    else 
                            echo "Error: The password is error."
                            echo 
                            read -s -p "Please input the password: " thePwd
                            clear
                    fi
                    pwdCount=`expr ${pwdCount} + 1`
            done
    }
    function autossh()
    {
            SRC_HOST="$1"
            SRC_PORT="$2"
            SRC_PWD="$3"
            commands="$4"
            expect -c "
                    set timeout 30
                    spawn  ssh -p $SRC_PORT root@$SRC_HOST
                    expect {
                              "(yes/no)?"                         {send "yes
    ";exp_continue}
                              "password:"                         {send "${SRC_PWD}
    ";exp_continue}
                              "Last login"                        {send "$commands
    ";}
                           
                           }
                           interact
                           #expect eof
            "
    }
    
    function synIPdata()
    {
           
            dataId="$1"
            saveFile="$2"
            tmpFile="`pwd`/tmp/sq_iplist_$RANDOM"
            if [ ! -d `dirname ${tmpFile}` ];then 
                mkdir `dirname ${tmpFile}`
            fi
            createFile $tmpFile
            echo -n "Sync ip list from main data......"
            mainDataUrl="this is a URL"
            curl "$mainDataUrl" -o $tmpFile >> /dev/null 2>&1
            if [ "$?" -ne 0 ]; then
                    echo -e "33[31;49;1mError: Some error occur when execute curl '$mainDataUrl' -o $tmpFile 33[39;49;0m"
                    exit 1
            fi
            echo -e "33[32;49;1mOK33[39;49;0m" 
            #列表解析
            echo -n "Analyse $tmpFile......"
            cat $tmpFile | dos2unix | grep '|||' | sed 's/^ *//g' | sed 's/ /*/g' | perl -p -i -e "s/(/--(/g" | perl -p -i -e "s/|||/
    /g"  | tr '|' '	'   | awk '{if ($1~/--(/) ; print $1,$2,$3,$4,$5,$6,$7,"@@"$1,$8} ' |  perl -p -i -e "s/@@.*--//g" |  awk '{if ($1~/--(/) gsub("--(", "*");sub("合区)", "");print $0} ' | sed 's/@@//g' | sed '/^s*$/d' | sed '/^s*#/d' > $saveFile
            if [ "$?" -ne 0 ]; then
                    echo -e "33[31;49;1mError: Some error occur when analyse $tmpFile 33[39;49;0m"
                    exit 1
            fi
            echo -e "33[32;49;1mOK33[39;49;0m" 
            rm -f $tmpFile
    }
    function sshIp()
    {
            autossh ${the_outer_ip} 16333 ${sshPwd} "export PS1='\u@\\[\e[0;31m\\]<${theSiteId}-${theServerName}>\\[\e[m\\]:\\\$'"
    }
    
    
    function serverList()
    {
            i=1
            createFile "${dir}/tmp/theServerList"
            cat ${ipListFile} |awk '{print $1}' |sort -u| while read theServer; do
                    echo "$((i++)) - ${theServer}"
            done > ${dir}/tmp/theServerList
            echo
            echo " Server Select"
            echo "------------------------------"
            cat ${dir}/tmp/theServerList
            echo
            echo "R - Return"
            echo "------------------------------"
            echo -n "Please chose the agent number : "
            read theServerNum
            while true; do
                    if [ "${theServerNum}" = "R"  -o  "${theServerNum}" = "r" ]; then
                            break
                    fi
                    isSelectOK=`cat ${dir}/tmp/theServerList | grep -E "^${theServerNum} - " | wc -l`
                    if [ "${isSelectOK}" -ne 0 ]; then
                            break
                    else
                            echo "Error: No such server number."
                            echo
                            echo -n "Please chose the server number: "
                            read theServerNum
                    fi
            done
            theServerName=`cat ${dir}/tmp/theServerList | grep "^${theServerNum} - " | awk -F" - " '{print $2}'`
    }
    
    function serverList1()
    {        if [ -n $1 ];then
                theServerName1=$1
            else
                echo 'The function serverList1 have not input variables!'
            fi
            
            i=1
            TemptheServerName=`echo ${theServerName}|tr '*' 'a'`
            createFile "${dir}/tmp/theServerList1"
            cat ${ipListFile}|tr '*' 'a' |grep "${TemptheServerName}"|awk '{print $2}' |sort -u| while read theServer1; do
                    echo "$((i++)) - ${theServer1}"
            done > ${dir}/tmp/theServerList1
            echo
            echo " Server Select"
            echo "------------------------------"
            cat ${dir}/tmp/theServerList1
            echo
            echo "R - Return"
            echo "------------------------------"
            echo -n "Please chose the agent number : "
            read theServerNum1
            while true; do
                    if [ "${theServerNum1}" = "R"  -o  "${theServerNum1}" = "r" ]; then
                            break
                    fi
                    isSelectOK=`cat ${dir}/tmp/theServerList1 | grep -E "^${theServerNum1} - " | wc -l`
                    if [ "${isSelectOK}" -ne 0 ]; then
                            break
                    else
                            echo "Error: No such server number."
                            echo
                            echo -n "Please chose the server number: "
                            read theServerNum1
                    fi
            done
            theServerName1=`cat ${dir}/tmp/theServerList1 | grep "^${theServerNum1} - " | awk -F" - " '{print $2}'`
            the_outer_ip=`cat ${ipListFile}|tr '*' 'a' |grep "${TemptheServerName}"|grep "${theServerName1}"|awk '{print $3}'`
    }
    
    
    
    
    function main()
    {
            if [ "$1" = "-h" -o "$1" = "--help" ]; then
                    echo "sqlist [syncIP/syncChinaForCheck/syncAbroadForCheck/syncAbroadOthers/syncAll] [toFile]"
                    exit 0
            fi
            init
            #执行该工具只同步IP列表,不进入选区菜单
            synIPdata 6 ${ipListFile}
            checkPwd
            isOnServerList=0
            stty erase ^h
            while true; do
                    if [ ${isOnServerList} -eq 0 ];then
                        serverList
                    fi
                    if [ "${theServerNum}" = "R"  -o  "${theServerNum}" = "r" ]; then
                            break
                    else 
                        serverList1 ${theServerName}
                        if [ "${theServerNum1}" = "R"  -o  "${theServerNum1}" = "r" ]; then
                            isOnServerList=0
                            continue
                        fi
                    fi
                    
                    sshIp
                    
                
            done
    }
    main
  • 相关阅读:
    vi编辑器命令大全
    Ubuntu环境搭建svn服务器
    Visual Studio中“后期生成事件命令行” 中使用XCopy命令
    解决 Visual Studio For Mac 还原包失败问题
    [Win10应用开发] 如何使用Windows通知
    [Win10应用开发] 使用 Windows 推送服务
    如何使用 PsExec 执行远程命令
    如何在调试Window App时,触发 Suspending ,Resuming 等事件
    如何 “解决” WPF中空域问题(Airspace issuse)
    浅谈可扩展性框架:MEF
  • 原文地址:https://www.cnblogs.com/gered/p/12200000.html
Copyright © 2011-2022 走看看