The UNIX and Linux Forums  

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 here.

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
Variable names Bab00shka UNIX for Dummies Questions & Answers 2 11-11-2005 07:45 AM
echo contents of differing variable names gefa Shell Programming and Scripting 2 05-24-2005 04:49 AM
KDE names gnerd What's on Your Mind? 1 07-18-2004 07:26 AM
Variable assignment for file names. jagannatha UNIX for Dummies Questions & Answers 2 05-29-2003 01:38 PM
Variable names Bab00shka Shell Programming and Scripting 4 08-15-2002 08:00 AM

Reply
 
LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 02-04-2008
Registered User
 

Join Date: Feb 2008
Posts: 2
awk computed variable names

Is there a way to do make-style computed variable names in awk? e.g. in make
Code:
foo = bar
bar = wocket
I can get "wocket" with
Code:
$($(foo))
Alternatively can you list all defined variables in awk?

thanks
Reply With Quote
Forum Sponsor
  #2 (permalink)  
Old 02-04-2008
Klashxx's Avatar
HP-UX/Linux/Oracle
 

Join Date: Feb 2006
Location: Almerķa, Spain
Posts: 371
Maybe this helps:
Code:
> foo=var
> awk 'BEGIN{var="wocket";print '${foo}'}'
wocket
Reply With Quote
  #3 (permalink)  
Old 02-04-2008
Moderator
 

Join Date: Dec 2003
Location: /ksh93
Posts: 883
The following code snippet outputs "wooket"

Code:
#!/usr/bin/awk -f

BEGIN {
    bar="wooket";
    foo=bar;
}

END { print foo }
Reply With Quote
  #4 (permalink)  
Old 02-05-2008
Registered User
 

Join Date: Feb 2008
Posts: 2
Thanks for the reply, but that's not exactly what I'm after. That just assigns "wocket" to bar, copies the value of that to foo, then prints the value of foo.

I was wondering if it's possible to say (as in a makefile)....
- assign the string "bar" to the variable foo.
- assign some string value to the variable bar.
- retrieve the value of a variable (in this case bar), where the name of the variable to retrieve from is specified in another variable (in this case foo).

So writing $($(foo)) accesses the value of a variable whose name is stored in foo.

Klashxx's one is closer to what I need, but it doesn't work if i move the foo=var from the shell into the awk script.

I guess this can't be done in awk. I'll workaround it a different way.
Thanks
Reply With Quote
  #5 (permalink)  
Old 02-05-2008
vgersh99's Avatar
Moderator
 

Join Date: Feb 2005
Location: Boston, MA
Posts: 2,999
there's no 'eval' in awk: FYI

Last edited by vgersh99; 02-05-2008 at 10:04 AM.
Reply With Quote
Google UNIX.COM
Reply

Thread Tools
Display Modes




All times are GMT -7. The time now is 07:44 PM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited.
The UNIX and Linux Forums Content Copyright ©1993-2008 The CEP Blog All Rights Reserved -Ad Management by RedTyger Visit The Global Fact Book

Content Relevant URLs by vBSEO 3.2.0