• 判断一个字符串形如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
  • 相关阅读:
    Hadoop集群(第3期)_VSFTP安装配置
    Hadoop集群(第5期)_Hadoop安装配置
    Hadoop集群(第6期)_WordCount运行详解
    Hadoop集群(第8期)_HDFS初探之旅
    Hadoop集群(第10期)_MySQL关系数据库
    Hadoop集群(第5期副刊)_JDK和SSH无密码配置
    Hadoop集群(第4期)_SecureCRT使用
    Hadoop集群(第9期)_MapReduce初级案例
    [winform]Value Object property expects either null/nothing value or int type
    【Winform】单元格的Formatted值的类型错误
  • 原文地址:https://www.cnblogs.com/super119/p/1910018.html
Copyright © 2020-2023  润新知