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 and shell scripting languages here.

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
global variable not being set robotball Shell Programming and Scripting 5 06-10-2008 07:20 AM
setting a global variable in script arunkumar_mca UNIX for Dummies Questions & Answers 3 10-23-2007 08:46 AM
Global Variable in a script? skyineyes Shell Programming and Scripting 2 07-12-2007 07:55 AM
Global Variable in awk... ZINGARO Shell Programming and Scripting 1 07-04-2007 02:07 PM
Global variable becomes local odashe318 Shell Programming and Scripting 2 10-29-2004 10:18 PM

Closed Thread
English Japanese Spanish French German Portuguese Italian Dutch Swedish Russian Norwegian Hungarian Hebrew Danish Bulgarian Greek Powered by Powered by Google
 
LinkBack Thread Tools Search this Thread Rate Thread Display Modes
  #1 (permalink)  
Old 03-16-2008
anhtt anhtt is offline
Registered User
  
 

Join Date: Nov 2007
Posts: 22
global variable in awk

I wrote a awk script file and define some global variables after BEGIN option:
BEGIN {
cell = "";
alarm = "";}

when i run the command:
awk -f awk_script inputfile
The results are as expected. But when I put awk script into a shell script. Global variables couldn't be understand.

I don't want to use the formatting:
awk -v cell=123 awk_script inputfile . Because the global variables can't be changed many times before finishing awk_script ( with -v option: golbal variables're alway fixed).

Who can solve this for us?
Thanks
  #2 (permalink)  
Old 03-16-2008
fpmurphy's Avatar
fpmurphy fpmurphy is offline Forum Staff  
Moderator
  
 

Join Date: Dec 2003
Location: Florida
Posts: 1,941
awk does not have the concept of immutable global variables.

If you define a variable such as cell in the BEGIN { cell=""; } rule, this variable will be set before any other rules are applied. However if you include a variable assignment on the command line i.e. -v cell=123, then cell is set to 123 before the BEGIN rule is applied. When the BEGIN rule is applied, then cell is (re)set to "".
Closed Thread

Bookmarks

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 08: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