0

I have seed file with below content.

$cat seed.txt
macmachine,192.168.2.4,29772
windowsmachine,192.168.2.5,29772
$

When I run for loop first $x will cat first field of first line that is macmachine from seed file and "do" run second for loop and this will cat second field of first line and so on...

Here is my for loop

for x in `cat seed.txt |cut -d ',' -f1`; do
    for y in `cat seed.txt |cut -d ',' -f2`; do
        curl -v -H -X GET -H "X-TEST-Request-Id: Domain-$x" http://$y:8080/static/test/$y.jsp;
    done;
done

The problem is it runs 8 times. If I remove first line from seed file it will run only once.

I'm sure something is wrong with the loop, but I'm not able to trace that. Please suggest how to fix it.

5
  • 1
    8=2 x 2 x 2; are you sure you don't have for z in cat seed.txt |cut -d ',' -f3; do Commented May 20, 2016 at 19:11
  • You shouldn't be using cat (UUoC — Useless Use of cat; search for it); cut is quite capable of reading files. Commented May 20, 2016 at 19:11
  • With two lines of data, you should be getting the curl executed 4 times. The outer loop has x set to macmachine then windowsmachine; the inner loop has y set to 192.168.2.4 and then 192.168.2.5. Did you omit a loop on z picking up the last column? That would give you 8 loops, but the code doesn't show that. Commented May 20, 2016 at 19:13
  • @JonathanLeffler last column is not required. only starting 2 field is required. Commented May 20, 2016 at 19:33
  • Check your file for special characters: cat -A seed.txt Commented May 20, 2016 at 19:47

2 Answers 2

1

When you want to proces one line after another, do something like

while IFS=, read -r x y z; do
   curl -v -H -X GET -H "X-TEST-Request-Id: Domain-$x" "http://$y:8080/static/test/$z.jsp"
done < seed.txt
Sign up to request clarification or add additional context in comments.

4 Comments

Walter thanks for your prompt reply. Where i need define "cat seed.txt |cut -d ',' -f1". also one more query how i can add if condition.. the condition is if first line curl is success then proceed to next curl if failed then break.
How i can pick random field?
When curl (or most other commands) fail, $? will be set to a value different from 0 (until the next command). You can add if [ $? -ne 0 ]; then break; fi after the curl line. Shorter is appending || break at the end of the curl command.
What do you mean with random field? Field of a line? Linenumber> Something else? For a random value between 0 and 3, use (( myrandom = RANDOM % 3 )) ; echo ${myrandom}.
0

did you tried to make something like this script does?

for x in `cat seed.txt` ; do
curl -v -H -X GET -H "X-TEST-Request-Id: Domain-$(echo $x | cut -f 1 -d ',')" http://$(echo $x | cut -f 2 -d ','):8080/static/test/$(echo $x | cut -f 2 -d ',').jsp
done

"for" statement reads each line of file in its respective iteration cycle (two times in this case) and assign a line (or text item separated by spaces to be precise) from file to x variable so many times as many text items are in the file. The thing that have to be done next is to split input line(x variable) onto substring variables and supply them to curl, which is done by the "echo|cut" command.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.