Awk

Mark Kennedy mek at michael.udev.cdc.com
Fri Mar 8 05:32:11 AEST 1991


In article <1991Mar7.115420.21315 at daimi.aau.dk>, ezra at daimi.aau.dk (Thomas Ravnholt) writes:
|> Hello !
|> 
|>   I have a little question about awk (nawk).
|> 
|>   If I want to run a unix-command in an awk-script,
|> how do I get the output into a variable.
|> 
|>   I tried
|> 
|>      getline < system(unixcommand)
|> 
|>      system(unixcommand | getline)
|> 
|> 
|> but it is no good of course. system returns 0 or 1 and
|> not the output of the unixcommand.
|> 

Crude, but effective, temporary files are your friend.  Here's a
simple example that does what you ask.

#! /bin/sh

awk '
BEGIN {
system("date > /tmp/foo")
getline X < "/tmp/foo"
print X
}'

You can use the pid if you are worried about stepping on duplicate
temp file names and insert a "trap" command to delete your temp
file(s) in case your script terminates prematurely.

     -Mark


-- 
                           ___  ___ 
                          / __||__ \ 
Mark Kennedy             ( |__  __| )  AT&T: (612) 482-2787
Control Data Corporation  \___||___/   E-Mail: mek at udev.cdc.com
			check-ins happen



More information about the Comp.unix.programmer mailing list