Unix/Linux Go Back    


Fedora is an operating system centered on the Linux open source kernel and is developed by the community-supported Fedora Project. Fedora is sponsored by Red Hat. Fedora contains software distributed under a free and open-source license. Fedora focuses on innovation, integrating and adopting new technologies and working closely with various Linux communities. The default desktop in Fedora is the GNOME desktop environment and the default user interface is the GNOME Shell. Other desktop environments include KDE Plasma, Xfce, LXDE, MATE and Cinnamon. Security is an important aspect of Fedora with one specific security feature, Security-Enhanced Linux, implementing a variety of software security policies, including mandatory access controls.

Shell Script - awk, begin, for and print

Fedora


Closed    
 
Thread Tools Search this Thread Display Modes
    #1  
Old Unix and Linux 08-30-2013
agriz agriz is offline
Registered User
 
Join Date: Aug 2011
Last Activity: 1 September 2013, 12:30 AM EDT
Posts: 8
Thanks: 0
Thanked 0 Times in 0 Posts
Shell Script - awk, begin, for and print


Code:
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?
Sponsored Links
    #2  
Old Unix and Linux 08-30-2013
Corona688 Corona688 is online now Forum Staff  
Mead Rotor
 
Join Date: Aug 2005
Last Activity: 26 April 2017, 11:49 AM EDT
Location: Saskatchewan
Posts: 22,081
Thanks: 1,071
Thanked 4,151 Times in 3,841 Posts
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.


Code:
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.
Sponsored Links
    #3  
Old Unix and Linux 08-30-2013
agriz agriz is offline
Registered User
 
Join Date: Aug 2011
Last Activity: 1 September 2013, 12:30 AM EDT
Posts: 8
Thanks: 0
Thanked 0 Times in 0 Posts
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
    #4  
Old Unix and Linux 08-30-2013
RudiC RudiC is offline Forum Staff  
Moderator
 
Join Date: Jul 2012
Last Activity: 26 April 2017, 8:00 AM EDT
Location: Aachen, Germany
Posts: 10,686
Thanks: 263
Thanked 3,283 Times in 3,024 Posts
pointsb will hold all the x-y coordinates of this inverse parabola, as printed out by print x,y, separated by <newline> chars.
Sponsored Links
    #5  
Old Unix and Linux 08-31-2013
agriz agriz is offline
Registered User
 
Join Date: Aug 2011
Last Activity: 1 September 2013, 12:30 AM EDT
Posts: 8
Thanks: 0
Thanked 0 Times in 0 Posts

Code:
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?

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

Sponsored Links
    #6  
Old Unix and Linux 08-31-2013
RudiC RudiC is offline Forum Staff  
Moderator
 
Join Date: Jul 2012
Last Activity: 26 April 2017, 8:00 AM EDT
Location: Aachen, Germany
Posts: 10,686
Thanks: 263
Thanked 3,283 Times in 3,024 Posts
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.
Sponsored Links
    #7  
Old Unix and Linux 08-31-2013
agriz agriz is offline
Registered User
 
Join Date: Aug 2011
Last Activity: 1 September 2013, 12:30 AM EDT
Posts: 8
Thanks: 0
Thanked 0 Times in 0 Posts
Quote:
Originally Posted by RudiC View Post
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?
Sponsored Links
Closed

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Linux More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
How to print backslash in shell script using awk? wxuyec Shell Programming and Scripting 8 01-30-2013 03:37 PM
strange: sed and awk print at end instead of begin of line regisl67 Shell Programming and Scripting 5 10-18-2011 12:10 PM
awk/shell script to print each line to a file saint2006 Shell Programming and Scripting 3 01-06-2010 10:26 AM
shell script(Preferably awk or sed) to print selected number of columns from each row ks_reddy Shell Programming and Scripting 3 08-25-2009 07:39 AM
awk getting stuck after BEGIN akshaykr2 Shell Programming and Scripting 1 06-24-2009 01:04 PM



All times are GMT -4. The time now is 11:50 AM.