The UNIX and Linux Forums  
Hello and Welcome from United States to the UNIX and Linux Forums! Thank You for Visiting and Joining Our Global Community.

Go Back   The UNIX and Linux Forums > Top Forums > Shell Programming and Scripting
.
google unix.com



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

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
Development Releases: Linux Mint 4.0 Beta "Fluxbox", 4.0 Alpha "Debian" iBot UNIX and Linux RSS News 0 01-04-2008 03:00 PM
Explain the line "mn_code=`env|grep "..mn"|awk -F"=" '{print $2}'`" Lokesha UNIX for Dummies Questions & Answers 4 12-20-2007 01:52 AM
how could i make a program mixed with many "|", "<" and ">" strugglingman High Level Programming 2 04-29-2006 08:11 AM
No utpmx entry: you must exec "login" from lowest level "shell" peterpan UNIX for Dummies Questions & Answers 0 01-18-2006 04:15 AM
"Global" profiles? amheck UNIX for Dummies Questions & Answers 6 12-04-2005 08:25 PM

Closed Thread
English Japanese Spanish French German Portuguese Italian Dutch Swedish Russian Norwegian Hungarian Hebrew Danish Powered by Powered by Google
 
LinkBack Thread Tools Search this Thread Rate Thread Display Modes
  #1 (permalink)  
Old 05-26-2006
petoSVK petoSVK is offline
Registered User
  
 

Join Date: May 2006
Posts: 20
AWK -> getting "global" vars

Hello,

presently, I'm calling nawk from my main script and have nawk defined in one file. So I call nawk like this
nawk -f file input

This file defines how to separate mails in /var/mail/$user and show 1 at a time. However, I would also like to do other actions (delete message, forward message, etc), but I don't want to do separately. I mean to create a new "nawk" file just for forwarding... .

The thing I would like is to pass a variable into this nawk file so I can decide what to do (print, forward, delete) and have it in one place. But the problem is I have no clue how to pass info and get it in nawk file.

I hope there is someone with a clue.

Thx.

Peto
  #2 (permalink)  
Old 05-26-2006
vgersh99's Avatar
vgersh99 vgersh99 is online now Forum Staff  
Moderator
  
 

Join Date: Feb 2005
Location: Boston, MA
Posts: 5,119
nawk -v action='print' -f file.awk input.txt

file.awk:
Code:
BEGIN {
   # if no action is specified on the command line - default to "print"
   if (action == "")
     action="print"
}
{
   if ( action == "print" )
       doPrint
   if ( action == "forward" )
      doForwarding
   if ( action == "whatever" )
      doWhatever   
}

Last edited by vgersh99; 05-26-2006 at 12:55 PM..
  #3 (permalink)  
Old 05-26-2006
petoSVK petoSVK is offline
Registered User
  
 

Join Date: May 2006
Posts: 20
Exactly what I wanted...

Thanks a lot...

Peto
  #4 (permalink)  
Old 05-26-2006
petoSVK petoSVK is offline
Registered User
  
 

Join Date: May 2006
Posts: 20
Is it possible to get a value from NAWK?
so I can print every message and let user choose which mail to delete and after that I would get which mail he chose and after that I would call nawk again with action=delete mail=value...
  #5 (permalink)  
Old 05-26-2006
vgersh99's Avatar
vgersh99 vgersh99 is online now Forum Staff  
Moderator
  
 

Join Date: Feb 2005
Location: Boston, MA
Posts: 5,119
Quote:
Originally Posted by petoSVK
Is it possible to get a value from NAWK?
so I can print every message and let user choose which mail to delete and after that I would get which mail he chose and after that I would call nawk again with action=delete mail=value...
Code:
#!/bin/ksh
eval $(nawk -v action='choose' -f file.awk input.txt)
echo "ChosenRecord->[${chosenRecord}]"
file.awk:
Code:
BEGIN {
   # if no action is specified on the command line - default to "print"
   if (action == "")
     action="print"
}

function doChoose()
{
   # here we're doing the "choosing" - Once a record is chosen...
    printf("chosenRec=%d\n", numberOfChosenRecord);
}
{

   if ( action == "choose" )
       doChose

   if ( action == "print" )
       doPrint
   if ( action == "forward" )
      doForwarding
   if ( action == "whatever" )
      doWhatever   
}
  #6 (permalink)  
Old 05-27-2006
petoSVK petoSVK is offline
Registered User
  
 

Join Date: May 2006
Posts: 20
When I write
eval $( nawk -f forward.awk /var/mail/$USER )
it wants more input so I have to end it with double ^D .

Also I'm not sure if I understand it correctly. What does eval do in this example? Does it return value which was returned from nawk? so I can get it by using $? .
How nawk return a value? : return 55 ?

Is it possible to use "global variables" that I can modify also in nawk and have it changed in parent script.
Closed Thread

Bookmarks

Tags
bash, bash eval, eval

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On




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


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited. Language Translations Powered by .
vBCredits v1.4 Copyright ©2007 - 2008, PixelFX Studios
The UNIX and Linux Forums Content Copyright ©1993-2009. All Rights Reserved.Ad Management by RedTyger

Content Relevant URLs by vBSEO 3.2.0