2

I have a C application called Test. Test takes in a single int parameter. I want to run Test with many different parameters, so I made the following bash script:

#!/bin/bash
for i in {0..5}
do
    ./Test "$i" 
done

However, this executes ./Test "$i" and not ./Test 0, ./Test 1 etc. Changing it to ./Test $i simply executes ./Test $i 5 times.What am I doing wrong?

2
  • 1
    Your explanation of the problem is wrong. Replace test with /bin/echo and see that it works. There shall be some other problem, probably in ./Test itself. Commented Jan 26, 2013 at 8:20
  • I think you're right. /bin/echo prints out the correct values of i: 0 to 5. I'll double check the C program. Thanks! Commented Jan 26, 2013 at 8:22

2 Answers 2

1

Try

#!/bin/bash
for i in `seq 0 5`
do
    ./Test $i 
done

To create sequences in bash you can use seq, which has an advantage of working in any bourne-compatible shell (that is, seq works everywhere because it's an external program, it's the for loop that becomes more portable).

Yes, it makes no difference with respect to ./Test invocation, but it provides a respectful reason to procrastinate before fixing ./Test.

Sign up to request clarification or add additional context in comments.

4 Comments

You can use {} just as well. What's the point of advising to try this and that if you don't understand what's wrong? (It would help if you had at least an idea on what's wrong. Not necessary correct idea, just any idea, without "try random things doing the same" nonsense).
Same version here, works as documented (it's "Brace Expansion", it was there for years. Just tried it in 3.2.48 and it works too)
You're right. I'm wrong. Sorry, still I'd consider using seq as it also works with other shells (dash, for example).
The answer is better now. Thanks.
1
for i in {0..5}
do
    ./Test " $i" 
done

Or

for i in {1..5}; do exec "./Test $i" ; done

2 Comments

That simply executes ./Test $i 5 times, the value of i is still not being replaced..
@JayMahendru gave you two solutions... you missed the space

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.