-1

I have a scenario of doing a curl request with payload from a file to my server. Here I need to replace values in my file incrementing values by 1000 and repeating the same for 25 times. I am able to replace the values by 'sed' but I am not able to loop it for 25 times. Here is what I implemented for one time.

curl -H "text/xml" --data-binary "@/home/miracle/email/somainput1.xml" https://x.x.x.x:5550 --insecure -u admin:xxxxx >> somaoutput1.xml

my input file has the following code..

    <soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/">
<soapenv:Body>
<dp:request domain="HUB" xmlns:dp="http://www.datapower.com/schemas/management">
<dp:b2b-query-metadata>
<dp:query>
<dp:query-condition evaluation="property-equals">
<dp:property-name>ResultCode</dp:property-name>
<dp:value>0</dp:value>
</dp:query-condition>
<dp:query-condition evaluation="logical-and">
<dp:query-condition evaluation="property-greater-than">
<dp:property-name>InputTime</dp:property-name>
<dp:value>2019-02-19 23:00:00</dp:value>
</dp:query-condition>
<dp:query-condition evaluation="property-less-than">
<dp:property-name>InputTime</dp:property-name>
<dp:value>2019-02-20 11:00:00</dp:value>
</dp:query-condition>
</dp:query-condition>
</dp:query>
<dp:result-constraints>
<dp:max-rows>1000</dp:max-rows>
**<dp:start-index>18001</dp:start-index>**
<dp:include-properties>
<dp:property-name>SenderName</dp:property-name>
<dp:property-name>ReceiverName</dp:property-name>
<dp:property-name>ResultCode</dp:property-name>
</dp:include-properties>
</dp:result-constraints>
</dp:b2b-query-metadata>
</dp:request>
</soapenv:Body>
</soapenv:Envelope>

and I can able to replace it by sed using

sed -i '23s/18001/19001/g' b2bsoapinput.xml

I need to do the loop and send the same curl request 25 times.

0

A command you could loop over is

sed -n '23s/[^0-9]*\([0-9]*\)[^0-9]*/\1+1000/p' \
b2bsoapinput.xml | bc | xargs -I{} sed -i \
'23s/\([^0-9]*\)[0-9]*\([^0-9]*\)/\1{}\2/g' b2bsoapinput.xml

So you could run

for i in {1..25}; do
    curl -H "text/xml" --data-binary \
    "@/home/miracle/email/b2bsoapinput.xml" \
    https://x.x.x.x:5550 --insecure -u admin:xxxxx \
    >> somaoutput$i.xml || break
    sed -n '23s/[^0-9]*\([0-9]*\)[^0-9]*/\1+1000/p' 
    b2bsoapinput.xml | bc | xargs -I{} \
    sed -i '23s/\([^0-9]*\)[0-9]*\([^0-9]*\)/\1{}\2/g' \
    b2bsoapinput.xml
done

The first sed matches the only number in line 23 and replaces it with number+1000. The .../p tells sed to print the replaced value and -n tells it to not print the changed file. Hence only the replacement string is piped on to bc wich does the aritmetics. The second sed replaces the only number in line 23 with the new value and changes the file in place. I used xargs to put the piped value into the sed argument. The -I{} sets the string that is replaced by the piped value to {}.

Edit: I added a || break in the loop. This causes the loop to "stop" if the preseeding command fails (returns an exit code other than 0) and the program to continue after the loop. However, if curl does exit with 0 even when it receives an error response you need to call break on another condition. Maybe something like grep "an error text" somaoutput$i.xml && break.

If you want your program to continue with the next iteration of the loop instead of after it you can use continue instead of break.

  • It is my pleasure :) – katosh Mar 1 at 12:01
  • Hello Katosh, for above code I have to check a condition before looping the increments depends on the response code. Lets say we have a response at 22nd request as "request exceeds the limit" then it should stop increments and executes next command. – harishchowdary Mar 4 at 19:10
  • So you want to skip the number if curl fails? – katosh Mar 4 at 19:16
  • yes, and also need to do the same calls for 4 different servers – harishchowdary Mar 4 at 19:19
  • In your questions, you stated the number in the downloaded file should always be incremented by 1000. No matter what that number is. If you want to have curl retry until it succeeds I could edit that in my answer. What exactly should happen when it fails? – katosh Mar 4 at 19:39
0

Do like this, its basic loop

#!/bin/bash

for i in {1..25}; do
        # put the code you want to loop 25 times here
done

Or oneline:

$ for i in {1..25}; do #your code here ; done

To increment values and substitute them, try this:

!/bin/bash 

counter=1 
for i in {1..25}; do    
         # Send CURL request    
         curl -H "text/xml" --data-binary \     
         "@/home/miracle/email/b2bsoapinput.xml" \  
         https://x.x.x.x:5550 --insecure -u admin:xxxxx \   
         >> somaoutput$i.xml    

         # inc_counter will hold values 1000 bigger than counter    
         # we will replace value of counter with inc_counter in text file   
         inc_counter=$(expr $counter + 1000)    

         # replace values   
         sed -i "23s/$counter/$inc_counter/g" b2soapinput.xml   

         # Increment counter by 1000    
         let "counter+=1000" 
done
  • Thank You for this. But how about replacing the incremented values with 1000 starting from 1 to 25001 – harishchowdary Mar 1 at 11:34
  • @harishchowdary i edited the answer – Tryna Learn Somethin Mar 1 at 11:56
  • Thank you....What if I want to loop and increment the count by 1000 based on the curl response? – harishchowdary Mar 5 at 9:29
  • This is gonna start from 1 and with each request in text file will substitute the value with the one that is 1000 greater – Tryna Learn Somethin Mar 5 at 10:07
  • yes I understand, but I want the loop to be incremented based on the curl response. Means I want to validate the response, upon that needs to increment the loop – harishchowdary Mar 5 at 10:27

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.