Tell me more ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

I have to send some cURL request in a parallel way. At one side I have the curl.sh script (for security reasons data has been hidden):

#!/bin/bash

SERVER=...
PORT=...
CONTEXTPATH=...
SERVICE=...
CERTIFICATETYPE=...
CERTIFICATE=...
KEYCERT=...
HEADER=...
CONTENT="Content-Type:text/xml;charset=UTF-8"

if [ "$1" != "" ] && [ "$2" != "" ]
then
        echo "Executing curl script for $1 > $2"
        curl -s -d @$1 --cert $CERTIFICATE:$KEYCERT --cert-type $CERTIFICATETYPE http://$SERVER:$PORT/$CONTEXTPATH/$SERVICE -H $HEADER -H $CONTENT > $2
else
        echo "ERROR not valid arguments: curl.sh[ARG1=$1,ARG2=$2]"
fi

So this script, will be call as:

$ curl.sh requestData outputFile

Then, in another parallel.sh script I have:

#!/bin/bash
MODE=$1
REQUEST=

if [ "$MODE" != "" ]
then
        if [ $MODE -eq 1 ]
        then
                REQUEST=requestFileOne.xml
        fi
        if [ $MODE -eq 2 ]
        then
                REQUEST=requestFileTwo.xml
        fi
else
        echo "ERROR no valid parameter"
        echo "./parallel.sh 1|3"
        echo "  mode 1: send request 1x1"
        echo "  mode 3: send request 3x3"
fi

if [ "$REQUEST" != "" ]
then
        echo "START CURL PARALLEL for $REQUEST"

        time ./curl.sh $REQUEST ./out/out_1.xml & ./curl.sh $REQUEST ./out/out_2.xml &
        ./curl.sh $REQUEST ./out/out_3.xml & ./curl.sh $REQUEST ./out/out_4.xml &
        ./curl.sh $REQUEST ./out/out_5.xml & ./curl.sh $REQUEST ./out/out_6.xml

        wait
fi

echo "END CURL PARALLEL"

As you can see, now I do

$ ./parallel.sh 1

But if I want to change the number of parallel calls, I have to edit the parallel.sh script file, and I would want something like:

$ ./parallel.sh file number

Where {file: 1|2} is the data file to use with cURL, and {number:X} will be the number of parallel calls.

Firstly, I thought of building the parallel executing command inside a loop

for i in 1..$2
do
   stringVar="$stringVar | ./curl.sh $REQUEST ./out/output_$i.xml"
done

for i in 1..$2
do
   result=`./curl.sh $REQUEST ./out/output_$i.xml`
done

for i in 1..$2
do
   ./curl.sh $REQUEST ./out/output_$i.xml
done

But, obviously none of this loops works as expected. At first loop, I was trying to save the curls command as String, so later I will run the String as command.

Any idea? Thanks in advance.

share|improve this question
What about using getops? This way you can execute it like ./parallel.sh -f file -n number and define default values. – fedorqui 44 mins ago
That would be nice If I know how many times the cURL script must be called. But user may could use for just a single call (-n 1) [non-parallel], single pair (-n 2), two pairs (-n 4) and so on. – Wolfchamane 33 mins ago

Know someone who can answer? Share a link to this question via email, Google+, Twitter, or Facebook.

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Browse other questions tagged or ask your own question.