1

I would like to execute a script within the following loop.

x=10.000; nx=3; dx=0.125; y=15.000; ny=5; dy=0.120
for i in {1..3};do
for j in {1..5};do
xi=xi+($i-1)*dx
yj=yj+($j-1)*dy
echo $xi $yj #run the script which needs the values of xi and yj for each run
done
done

desire output

10.000 15.000
10.000 15.120
10.000 15.240
10.000 15.360
10.000 15.480
10.125 15.000
10.125 15.120
10.125 15.240
10.125 15.360
10.125 15.480
10.250 15.000
10.250 15.120
10.250 15.240
10.250 15.360
10.250 15.480
RavinderSingh13
  • 130,504
  • 14
  • 57
  • 93
Kay
  • 1,957
  • 2
  • 24
  • 46

2 Answers2

2

In awk could you please try following.

awk -v outer_start="10.000" -v outer_loop_times="3" -v outer_diff="0.125" -v inner_start="15.000" -v inner_loop_times="5" -v inner_diff="0.120" '
BEGIN{
    for(i=1;i<=outer_loop_times;i++){
        for(j=1;j<=inner_loop_times;j++){
            print outer_start,inner_start
            inner_start=sprintf("%.03f",inner_start+inner_diff)
        }
        outer_start=sprintf("%.03f",outer_start+outer_diff)
    }
}'
RavinderSingh13
  • 130,504
  • 14
  • 57
  • 93
1
x=10.000; nx=3; dx=0.125; y=15.000; ny=5; dy=0.120
for i in {1..3};do
    for j in {1..5};do
        xi=$( echo "$x+($i-1)*$dx" | bc )
        yj=$( echo "$y+($j-1)*$dy" | bc )
        echo $xi $yj
    done
done

You can use bc for floating-point arithmetic. Your equation had an issue too, it should be x and y instead of xi and yj

Also, I think you wish to use nx and ny, see Brace expansion with variable?

Sundeep
  • 23,246
  • 2
  • 28
  • 103
  • 1
    Yes, you are correct. I was looking for this only. Many thanks. As I need to pass the x and y values for each execution of my script. – Kay Nov 17 '19 at 09:02