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
  • 相关阅读:
    VS2008编写MFC程序--使用opencv2.4()
    November 02nd, 2017 Week 44th Thursday
    November 01st, 2017 Week 44th Wednesday
    October 31st, 2017 Week 44th Tuesday
    October 30th, 2017 Week 44th Monday
    October 29th, 2017 Week 44th Sunday
    October 28th, 2017 Week 43rd Saturday
    October 27th, 2017 Week 43rd Friday
    October 26th, 2017 Week 43rd Thursday
    October 25th, 2017 Week 43rd Wednesday
  • 原文地址:https://www.cnblogs.com/super119/p/1910018.html
Copyright © 2011-2022 走看看