ezra@daimi.aau.dk (Thomas Ravnholt) (03/07/91)
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. Hope you can answer this one. Thanks in advance ! ezra@daimi.aau.dk
mek@michael.udev.cdc.com (Mark Kennedy) (03/08/91)
In article <1991Mar7.115420.21315@daimi.aau.dk>, ezra@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@udev.cdc.com check-ins happen
steinbac@hpl-opus.hpl.hp.com (Guenter Steinbach) (03/12/91)
In comp.unix.misc, ezra@daimi.aau.dk (Thomas Ravnholt) writes: > 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. In nawk, I do "unixcommand" | getline x or even "unixcommand" arguments_from_awk | getline x to stuff the output of unixcommand into variable x. Good luck. Guenter Steinbach gunter_steinbach@hplabs.hp.com