How to pass nawk variable to shell within the same script? | Unix Linux Forums | Shell Programming and Scripting

  Go Back    


Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts and shell scripting languages here.

How to pass nawk variable to shell within the same script?

Shell Programming and Scripting


Closed Thread    
 
Thread Tools Search this Thread Display Modes
    #1  
Old 02-04-2013
Optimus81 Optimus81 is offline
Registered User
 
Join Date: Dec 2012
Last Activity: 17 May 2014, 6:04 PM EDT
Posts: 67
Thanks: 21
Thanked 0 Times in 0 Posts
How to pass nawk variable to shell within the same script?

Hi All,
I tried googling but so far no luck, can someone tell me how pass the variable value used inside the
nawk command to shell. In the below script i get the value of $c (without color: Total Executed: " c ")
but the printf which is outside the nawk command doesn't print the value or it always prints $c as null.

Code:
nawk 'BEGIN {
        print  "|========================================|"
        print "|   SI No     CHECKS           STATUS    |"
        print "|========================================|"
 } /^[0-9]/ {
        c++;
        print "|------------------------------------------|"
        printf "| %s ",$0; getline; s=$0; sub(/:.*/,"",s);
        if(s=="Passed") ++p; if(s=="Failed") ++f; if((s!="Passed")&& (s!="Failed")||(s==" ") ) ++n;
        printf " : %s   |\n", s;
        print "|------------------------------------------|"
 } END {
           print "|========================================|"
           print "| Total Executed: " c "                      |"
           print "|========================================|"
}' $1
 
        printf "| Total \e[1;35;40m Executed:  $c                   |\n"

Sponsored Links
    #2  
Old 02-04-2013
radoulov's Avatar
radoulov radoulov is offline Forum Staff  
Moderator
 
Join Date: Jan 2007
Last Activity: 1 September 2014, 10:03 AM EDT
Location: Варна, България / Milano, Italia
Posts: 5,667
Thanks: 182
Thanked 617 Times in 575 Posts
I would put the code inside the awk script:


Code:
nawk 'BEGIN {
        print  "|========================================|"
        print "|   SI No     CHECKS           STATUS    |"
        print "|========================================|"
 } /^[0-9]/ {
        c++;
        print "|------------------------------------------|"
        printf "| %s ",$0; getline; s=$0; sub(/:.*/,"",s);
        if(s=="Passed") ++p; if(s=="Failed") ++f; if((s!="Passed")&& (s!="Failed")||(s==" ") ) ++n;
        printf " : %s   |\n", s;
        print "|------------------------------------------|"
 } END {
           print "|========================================|"
           print "| Total Executed: " c "                      |"
           print "|========================================|"
           printf "| Total \033[1;35;40m Executed: %s\033[0m                 |\n", c
}' "$1"

The Following User Says Thank You to radoulov For This Useful Post:
Optimus81 (02-04-2013)
Sponsored Links
    #3  
Old 02-04-2013
Optimus81 Optimus81 is offline
Registered User
 
Join Date: Dec 2012
Last Activity: 17 May 2014, 6:04 PM EDT
Posts: 67
Thanks: 21
Thanked 0 Times in 0 Posts
Thanks radoulov. It works fine now.
can you please tell me how to print the string say "Passed" in green color. my output looks like this :
am not sure how to incoporate this in below line(if it's correct) :

Code:
if(s=="Passed") ++p; if(s=="Failed") ++f; if((s!="Passed")&& (s!="Failed")||(s==" ") ) ++n;

output :

Code:
|========================================|
|SI No CHECKS STATUS |
|========================================|
|1)Intercomponents Checking : Passed |  ------------------------ >> #how to print this string "Passed" in green color
|----------------------------------------------------|
|2)OS version Checking : Passed |
|----------------------------------------------------|
|3)Component registered : Failed | ------------------------------->> #how to print this "Failed" string in red color
|----------------------------------------------------|
|4)Verify Server Connection : Failed |
|========================================|
| Total Passed : 2 |
| Total Failed : 2 |
| Total executed: 4 |
|========================================|

    #4  
Old 02-04-2013
radoulov's Avatar
radoulov radoulov is offline Forum Staff  
Moderator
 
Join Date: Jan 2007
Last Activity: 1 September 2014, 10:03 AM EDT
Location: Варна, България / Milano, Italia
Posts: 5,667
Thanks: 182
Thanked 617 Times in 575 Posts
Try this:


Code:
nawk 'BEGIN {
        print "|========================================|"
        print "|   SI No     CHECKS           STATUS    |"
        print "|========================================|"
 } /^[0-9]/ {
        c++;
        print  "|------------------------------------------|"
        printf "| %s ",$0; getline; s=$0; sub(/:.*/,"",s);
        if (s == "Passed") { ++p; _s = "\033[1;32m" s "\033[0m" }
        if (s == "Failed") { ++f; _s = "\033[1;31m" s "\033[0m" }
        if((s!="Passed")&& (s!="Failed")||(s==" ") ) ++n;
        printf " : %s   |\n", _s;
        print "|------------------------------------------|"
 } END {
           print "|========================================|"
           print "| Total Executed: " c "                      |"
           print "|========================================|"
           printf "| Total \033[1;35;40m Executed: %s\033[0m                 |\n", c
}' "$1"

The Following User Says Thank You to radoulov For This Useful Post:
Optimus81 (02-04-2013)
Sponsored Links
    #5  
Old 02-04-2013
Optimus81 Optimus81 is offline
Registered User
 
Join Date: Dec 2012
Last Activity: 17 May 2014, 6:04 PM EDT
Posts: 67
Thanks: 21
Thanked 0 Times in 0 Posts
Thanks it works fine now.
Sponsored Links
Closed Thread

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
Pass parameter to nawk from shell script samer.odeh Shell Programming and Scripting 8 10-24-2011 06:02 AM
pass variable from awk to shell script user_prady Shell Programming and Scripting 3 04-17-2008 05:43 AM
Is it possible to pass variable from awk to shell script user_prady Shell Programming and Scripting 3 12-18-2007 09:06 AM
is it possible to pass external variable values to nawk? swamymns Shell Programming and Scripting 1 02-02-2006 05:13 AM
How to Pass variable to shell Script sam70 UNIX for Dummies Questions & Answers 5 08-23-2005 07:27 PM



All times are GMT -4. The time now is 05:05 PM.