This code can handle the following formats: Compute [1-64], compute [01-11], compute [11-99], compute [010-021], compute [1-021], Cu [01-64]-Ib, and other situations, can basically meet the 99% loop input string. #! /Bin/bash
Declare-I ifrom
Declare-I iend
Declare-I ifromlength
Declare-I iendlength
Declare-I
Ifromlength =-1
Iendlength =-1
Tmpstring = "compute [10-64]-Ib"
# Validate the string syntax
Echo $ tmpstring | grep-e "[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-Q "^ 0"; then
Ifromlength =$ {# fromindex}
Iendlength =$ {# endindex}
If [$ ifromlength-ne $ iendlength]; then
Echo "$ fromindex and $ endindex shocould have the same length, exiting ..."
Exit
Fi
# Prefixzero to indicate that we shoshould Add "0" to our hostnames
Prefixzero = "Y"
Fi
# Get Rid Of the prefix Zeros
Fromindex = $ (echo "$ fromindex" | grep-o "[^ 0] [0-9] *")
Endindex = $ (echo "$ endindex" | grep-o "[^ 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-n "0"
Done
Echo-N $ 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