Code sharing:-Using array assignment to implement while and shift functions

Source: Internet
Author: User

3-column array, ARRAYA,ARRAYB,ARRAYC;
Each one by one corresponds to the number of resources that the host and its hosts can create, and ARRAYC indicates the number of resources that need to be created;
Arraya= ("192.168.1.1" "192.168.1.2" "192.168.1.3" "192.168.1.4")
arrayb= ("1" "2" "1" "2")
arrayc= ("100" "200" "300" "400" "500" "600" "700")
Note that the previous situation ${#arrayC [@}}=7, which exceeds the arrayb of all the resources that can be created 1+2+1+2=6,
When you need to create a resource 700, the script automatically exits;
There are 2 workarounds, the first forward double for loop, from the first element round to the last element,
However, this method is inefficient, followed by the second method, the code also write less than dozens of lines;

For _host in ${array[a]};d o    j=${arrayb[0]}         FOR _RES IN ${ARRAY[C]};d o        ssh  ${_host} init    [[ $? == 0 ]] && {  ((j=j-1));echo  "Success";  } | |  { echo  "Fail"; exit 1; }    arrayc= (${array[@]:1})          if [[ ${j} ==  "0"  ]];then         [[ ${#arrayC [@]} ==  "0"  ]] && status=0 | |  { STATUS=1;break; }    esle         [[ ${#arrayC [@]} ==  "0"  ]] && status=0    fi          done            if [[  "${status}"  ==  "1"  ]];then             arraya= (${arraya[@]:1})          arrayb= (${arrayb[@]:1})                  if [[ ${#array [@]} ==  "0"  ]];then             [[  "${#arrayC [@]}"  !=  "0"  ]] && { echo  "limited resources, ${arrayc[@]} refused to create";exit 1; }          esle              continue        fi             else        { echo  " Initialized as required "; EXIT 0; }  &NBsp; fidone 


This article is from the "Ithandonglin" blog, make sure to keep this source http://ithandonglin.blog.51cto.com/12414114/1886176

Code sharing:-Using array assignment to implement while and shift functions

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.