How to use Shell Script to run my C program for different initial values

Oct 21, 2010 at 3:09pm
Hi all

I run my program prog.c in the following way :

$ ./prog 1 > output.txt

where 1 is a user defined initial value used by the program.

But now I want to run it for a thousand initial values, 1-1000, and store all the outputs in different files.
Like

1
2
3
4
5
6
7
$ ./prog 1 > output1.txt
$ ./prog 2 > output2.txt
$ ./prog 3 > output3.txt
.
.
.
$ ./prog 1000 > output1000.txt

I know that a shell script (using a for loop and a variable output file name) can be VERY useful.
But I dont know how to make it.
PLEASE HELP

Regards
Alice
Oct 21, 2010 at 3:54pm
Oct 24, 2010 at 2:32pm
I hate shell scripts, but here goes...
Most of this is pulled from the link Bazzy gave, so Bazzy++.

1
2
3
4
5
6
7
LIMIT=1000

for ((a=1; a <= LIMIT ; a++))  # Double parentheses, and "LIMIT" with no "$".
do
  ./prog $a > output$a.txt   # read below
done                           # A construct borrowed from 'ksh93'.


I'm not entirely sure that's correct. It may be more like output'$a'.txt. Notice the single quotes around $a. But even that I'm not sure of. As I said, I hate shell scripting. People say it's easy, but to me it looks dirty, awkward, and clumsy. If you're the one who wrote the program, why not just make IT do all this work?
Oct 24, 2010 at 5:44pm
AFAIK
1
2
3
4
5
in the script

output$a
output'$a'
output"$a"
result for a = 1

output1
output$a
output1

Other link ;^) http://tldp.org/LDP/abs/html/quotingvar.html
Topic archived. No new replies allowed.