Shell Script - awk, begin, for and print

pointsb=`awk -v a2="$a2" -v b2="$b2" -v c2="$c2" -v yb="$yb" -v yc="$yc" \
	'BEGIN { for (y=yc; y<=yb; y++) { x = a2*y*y+b2*y+c2; print x, y }; }'`

I am learning shell script. I was reading a script and got confused in this line.
I understood that awk is allowing to assign the variable.

But I don't know how this line working.
Can anyone explain it, please?

The BEGIN section is a section of code awk reads before it reads any files.

It is not running shell code, which is why it looks so different, it's running awk code, it's its own language.

for (y=yc; y<=yb; y++) { x = a2*y*y+b2*y+c2; print x, y };

If yc=0 and yb=4, it loops 5 times, with y=0, y=1, y=2, y=3, y=4, y=5, following the mathematical formula given to calculate new results for x, then prints the coordinates.

What is the job print x,y here?

and assuming that x becomes 25 at the end of the loop, what will be the value of pointsb

pointsb will hold all the x-y coordinates of this inverse parabola, as printed out by print x,y , separated by <newline> chars.

pointsb=`awk -v a2="$a2" -v b2="$b2" -v c2="$c2" -v yb="$yb" -v yc="$yc" \
	'BEGIN { for (y=yc; y<=yb; y++) { x = a2*y*y+b2*y+c2; print x, y }; }'`

can write it like this?

pointsb = ""
for (y=yc; y<=yb; y++) { 
    x = a2*y*y+b2*y+c2; 
    pointsb = "$pointsb $x $y "
}

No - your mixing up shell and awk , a program.
pointsb is a shell variable. The `...` is the old, deprecated form of command substitution. awk is the program being called, the -v... s are awk-variable-definitions used to transfer shell variables to awk variables, the literal string '...' is an awk script, and finally the awk output is being assigned to pointsb.

Is it not possible to write the line of code without awk then?

You could use shell arithmetics, but this is quite limited on some shells. On others, yes, you can.

I understood that pointsb will hold the values of awk's execution

pointsb = 'final result of awk'

What does that awk do?

It does some looping and prints x,y values. I assume it will be stored in pointsb.
so pointsb might be holding something like this.

pointsb = 1,2 4,5 2,3 5,6
is that right?

Or what does awk return to pointsb?
I am not able to guess this. How does the variable assign work here?

Read your shell's man page on command substitution.
Read the awk man page. It's a very powerful pattern scanning and text processing language.

Your replies saved lot of time in learning shell script and made life easier :wink:

  • I downloaded Fedora 19
  • I installed it in my laptop
  • I created that simple shell script and executed to know the pointsb value with some dummy values of other variables
  • I created another shell script without awk
pointsb = ""
for (y=yc; y<=yb; y++) { 
    x = a2*y*y+b2*y+c2; 
    pointsb = "$pointsb $x $y "
}

The logic is completely right in the above code apart from the system mistakes. I am sure i am not going to write any shell script. I just wanted to learn it. So when i read a script, i could understand the flow.

Have a good day!