A script that identifies a string, such as compute [1-64], and extracts three key values: compute, 1, and 64.

Source: Internet
Author: User
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

 

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.