zoukankan      html  css  js  c++  java
  • 判断一个字符串形如compute[164]并能取出compute, 1, 64三个关键值的一段脚本

    本代码能处理形如:compute[1-64], compute[01-11], compute[11-99], compute[010-021], compute[1-021], cu[01-64]-ib等多种情况,能满足基本上99%的这种循环输入字符串。 
    #!/bin/bash
    declare -i iFrom
    declare 
    -i iEnd
    declare 
    -i iFromLength
    declare 
    -i iEndLength
    declare 
    -i i

    iFromLength
    =-1
    iEndLength
    =-1
    tmpstring
    ="compute[10-64]-ib"

    # validate the string syntax
    echo $tmpstring | grep -"[0-9a-zA-Z_-]+\[[0-9]+-[0-9]+\][0-9a-zA-Z_-]*"
    if [ $? -ne 0 ]; then
      echo 
    "illegal string..."
      
    exit
    else 
      echo 
    "correct..."
    fi

    # split the string to get what we want
    mainpart_pre=${tmpstring%%\[*\]*}
    mainpart_suf
    =${tmpstring##*\[*\]}
    fromindex_tmp=${tmpstring##*\[}
    fromindex=${fromindex_tmp%%-*\]*}
    endindex_tmp
    =${tmpstring#*\[*-}
    endindex=${endindex_tmp%%\]*}


    echo 
    "mainpartpre is $mainpart_pre"
    echo 
    "mainpartsuf is $mainpart_suf"
    echo 
    "fromindex is $fromindex"
    echo 
    "endindex is $endindex"

    # if fromindex begins with zero, then fromindex and endindex must have same length
    if echo $fromindex | grep -"^0"; then
        iFromLength
    =${#fromindex}
        iEndLength=${#endindex}
        if [ $iFromLength -ne $iEndLength ]; then
          echo 
    "$fromindex and $endindex should have the same length, exiting..."
          
    exit
        fi
        
    # prefixzero to indicate that we should add "0" to our hostnames
        prefixzero="y"
    fi

    # get rid of the prefix zeros
    fromindex=$(echo "$fromindex" | grep -"[^0][0-9]*")
    endindex
    =$(echo "$endindex" | grep -"[^0][0-9]*")

    iFrom
    =fromindex
    iEnd
    =endindex
    if [ $iFrom -ge $iEnd ]; then
      echo 
    "illegal iFrom and iEnd..."
      
    exit
    fi

    for (( ; iFrom<=iEnd; iFrom++ ))
    do
      
    if [ "$prefixzero" == "y" ]; then
        
    # fromindex begins with 0
        echo -n ${mainpart_pre}
        
    for (( i=0; i<(iEndLength-${#iFrom}); i++ )) 
        do
          echo 
    -"0"
        done
        echo 
    -$iFrom
        echo 
    $mainpart_suf
      
    else
        
    # fromindex doesn't begin with 0, we can generate the hostname directly
        echo ${mainpart_pre}${iFrom}${mainpart_suf}
      fi  
    done
  • 相关阅读:
    Hbase 0.98集群搭建的详细步骤
    java使用Apache POI操作excel文件
    linux下用非root用户重启导致ssh无法连接的问题
    solr update
    solr(一)
    libreoffice
    git
    hbase基本操作
    http://webapp.docx4java.org/OnlineDemo/PartsList.html
    git
  • 原文地址:https://www.cnblogs.com/super119/p/1910018.html
Copyright © 2011-2022 走看看