Sed & awk, 2nd Edition by Dale Dougherty & Arnold Robbins

Sed & awk, 2nd Edition by Dale Dougherty & Arnold Robbins

Author:Dale Dougherty & Arnold Robbins [Dale Dougherty & Arnold Robbins]
Language: eng
Format: epub
Tags: COMPUTERS / Operating Systems / UNIX, Reference:Computers
ISBN: 9781449387006
Publisher: O'Reilly Media
Published: 2010-11-19T00:00:00+00:00


An Array of Environment Variables

The ENVIRON array was added independently to both gawk and MKS awk. It was then added to the System V Release 4 nawk, and is now included in the POSIX standard for awk. It allows you to access variables in the environment. The following script loops through the elements of the ENVIRON array and prints them.

# environ.awk - print environment variable BEGIN { for (env in ENVIRON) print env "=" ENVIRON[env] }

The index of the array is the variable name. The script generates the same output produced by the env command (printenv on some systems).

$ awk -f environ.awk DISPLAY=scribe:0.0 FRAME=Shell 3 LOGNAME=dale MAIL=/usr/mail/dale PATH=:/bin:/usr/bin:/usr/ucb:/work/bin:/mac/bin:. TERM=mac2cs HOME=/work/dale SHELL=/bin/csh TZ=PST8PDT EDITOR=/usr/bin/vi

You can reference any element, using the variable name as the index of the array:

ENVIRON["LOGNAME"]

You can also change any element of the ENVIRON array.

ENVIRON["LOGNAME"] = "Tom"

However, this change does not affect the user's actual environment (i.e., when awk is done, the value of LOGNAME will not be changed) nor does it affect the environment inherited by programs that are invoked from awk via the getline or system( ) functions, which are described in Chapter 10.

This chapter has covered many important programming constructs. You will continue to see examples in upcoming chapters that make use of these constructs. If programming is new to you, be sure you take the time to run and modify the programs in this chapter, and write small programs of your own. It is essential, like learning how to conjugate verbs, that these constructs become familiar and predictable to you.



Download



Copyright Disclaimer:
This site does not store any files on its server. We only index and link to content provided by other sites. Please contact the content providers to delete copyright contents if any and email us, we'll remove relevant links or contents immediately.