4

I wrote a little bash script (my first) that does the following:

sed -e 's/Alpha=0/Alpha=x/' -e 's/Beta=0/Beta=y/' <file.pov >tmpfile.pov povray Width=480 Height=360 +Itmpfile.pov +Ofile_x_y.png 

everything works as intented but now I would like to pack these two lines in a loop for x=0:30:180 and y=0:30:90 (edit: I mean all possible combinations of x in {0,30,60,90,120,180} and y in {0,30,60,90}).

So for example for x=60 and y=30 the code should behave like this:

sed -e 's/Alpha=0/Alpha=60/' -e 's/Beta=0/Beta=30/' <file.pov >tmpfile.pov povray Width=480 Height=360 +Itmpfile.pov +Ofile_60_30.png 

I am aware, it should not be too hard, but for some reason, I just couldnt work it out by myself.

Sorry to bother you with my newbie questions!

2
  • you probably meant 's/Alpha=0/Alpha=x/' Commented Jul 6, 2015 at 16:32
  • 1
    Edit your question to show the full set of expected output given those x and y values as it's not clear if you want the output to include all possible combinations of both or if they should be one-to-one matched, nor is it clear even what you mean by x=0:30:180 and and y=0:30:90. Are you starting at zero and incrementing both by 30 but stopping at 180 for x and 90 for y or something else and how do the values relate to each other? Commented Jul 6, 2015 at 16:35

2 Answers 2

1

Use seq with for loops:

for x in `seq 0 30 180` do for y in `seq 0 30 90` do sed -e 's/Alpha=0/Alpha='$x'/' -e 's/Beta=0/Beta='$y'/' <file.pov >tmpfile.pov fn="file_${x}_${y}.png" povray Width=480 Height=360 +Itmpfile.pov +O${fn} done done 

seq is a great tool even if you don't need the numbers

for i in `seq 10` do call_me_ten_times done 
Sign up to request clarification or add additional context in comments.

3 Comments

I think there was a problem with the ${fn} using $x_$y. It tries to read $x_ so I changed that to ${x}_${y}. I tried the sed with a simple file containing Alpha=0 and Beta=0. But I forgot the first -e. Sorry. Now it works.
I use shells without these number features {0..30..180}. So I learned to like seq.
It doesn't matter for the example, but the for loop runs about 2 times faster than a read for big numbers.
1

here is a tricky bash hack, but it helps to reduce two nested for loop into a single while loop:

while read x y; do sed -e 's/Alpha=0/Alpha='"$x"'/' -e 's/Beta=0/Beta='"$y"'/' <file.pov >"tmpfile${x}_$y.pov" done < <(echo {0..180..30}' '{0..90..30} | tr ' ' '\n' | paste - -) 

the process substitution at last generates all combinations of the range you specified.

one remaining problem should be the output file name. since you are overwriting the file every time. i think it would be better if you make the filename changes as it goes. perhaps in my way.

1 Comment

thank you, it works like a charm! i dont use different output files because i clean them up after all the rendering is done anyways.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.