Pick up -4-shell in the flowers

Source: Internet
Author: User

Introduction

Shell, we often use, with its powerful features, will help us solve a lot of difficult problems. Recently encountered a problem, to run a lot of case, assuming that the serial execution, it takes a long time. Can we get them to go parallel, but not all of them in parallel? , because all cases are executed at the same time, the machine will be hung off.


1, Way 1

A straightforward approach is to maintain two file queues (*.start and *.stop) that record the execution state of all the case, and then obtain and allocate resources based on the number of concurrent quantities.

The code is as follows:

Multi.sh:

#!/bin/bash#rill Create to run cases parallel#2014-05-22 #mkdir./case_status#declare-i pc_numreadonly pc_num = 3case_lis t = "A b c d E F" function get_start_num () {num = 0for var in $case _listdoif [-e $var. start-a-F $var. start]; thennum = Nu M + 1fidonereturn $num}function get_case_stop () {case $ in "a") echo ' Get case: ' not stop ' rerurn 0;; B ") echo" Get Case: "Stop" return 1;; " C ") echo" Get case: $ not stop "Rerurn 0;;" D ") echo" Get Case: "Stop" Rerurn 1;; " E ") echo" Get Case: "Stop" Rerurn 1;; " F ") echo" Get Case: "Stop" Rerurn 1;; *) echo "Case $ not exist" exit 1;;;} For each_d in ${case_list};d oif [get_start_num-lt $pc _num];thenif [!-e $each _d.start]; thenif [!-e $each _d.stop];  Thentouch $each _d.start#start One new Caseelseecho "$each _d already stoped" rm $each _d.startfielseif [!-e $each _d.stop]; Thenecho "$each _d running ..." if[get_case_stop $each _d eq 1];thentouch $each _d.stoprm $each _d.startfielseecho "$each _d error! " Fififidone

It is important to note that in such a way, the end state of each case needs to be obtained, which can be obtained by parsing the output log at the end of a case execution.

Although there are powerful tools such as awk, it is still a daunting task to analyze the completion information for different cases.

Is there any other way?

Yes.


2, Way 2

Detailed analysis of all the cases started, the end of the scene, found and FIFO file characteristics very similar, so think of FIFO to achieve concurrency control.

For example, the following:

Multi.sh:

#!/bin/bash#rill Create to run cases parallel#2014-05-22 case_list= "a b c D e F g h i j k l m n o" readonly parallel_num=3r Eadonly fifo_id=9readonly fifo_name=fd2readonly log_name=log.log#create log fileif [-e ${log_name}-a-f ${log_name}];th Enrm-f ${log_name}fitouch ${log_name}echo "All cases begin time:$ (date +%y-%m-%d-%h:%m:%s)" >>${log_name} #create FIFO Fileif [-e ${fifo_name}-a-f ${fifo_name}];thenrm-f ${fifo_name}fimkfifo ${fifo_name} #bind FIFO to Fifo_ideval "E Xec ${fifo_id}<>${fifo_name} "#init fifofor ((idx=0;idx<${parallel_num};idx=idx+1)) Doecho-n-E" 1\n "> >${fifo_name}done#multi main bodyfor each_case in ${case_list};d oread-u ${fifo_id}{echo "${each_case} start:$ (date + %y-%m-%d-%h:%m:%s) ">>${log_name}sleep 1 #case runningecho" ${each_case} stoped:$ (date +%y-%m-%d-%h:%m:%s) "> >${log_name}echo-ne "1\n" >>${fifo_name}} &done#wait All the cases Stopedwaitecho "all cases finish time:$ (d Ate +%y-%m-%d-%h:%m:%s) ">>${loG_name} #remove the Fiform-f ${fifo_name} 


It is possible to find out that we do not have to worry about getting the end state of the case.

The following is the result of the execution, a total of 15 case, each case executed 1 seconds, the concurrent number is set to 3, all the case to complete the need of about 6.4 seconds.





3,shell Number of parameters passed

Usually when we use shell scripts, we often have to specify the parameters to the script, which can be written directly behind the command line, but this is very strong in the order of the parameters, it is more difficult to use.

We are able to do this by adding markers in front of the parameters.


#!/bin/bash## Shell test# rill# 2014-09-28opr1=xopr2=xopr3=xwhile [-n] $ (echo $ | grep '-') '];d o case $    in-h |--help)    echo "./test.sh-opr1 a-opr2 B-OPR3 C"    exit 0    ;; o PR1)    opr1=$2    shift    ;-O PR2)    opr2=$2    shift    ;-O PR3)    opr3=$2    shift    ;    Esac        Shiftdoneecho "Opr1=${opr1}  Opr2=${opr2}  OPR3=${OPR3}"


Validation results:




4, summary

The shell has not been used for a long time, and this summary will be "picking up flowers".


Pick up -4-shell in the flowers

Related Article

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.