![]() |
|
|
|
|
|
|
4
31st October 18:08
External User
Posts: 1
|
Please don't top-post. See below.
I meant to say the second time have_program is invoked, it returns Normally, it's good practice to define a string to hold the full command name and use that string for both the open and close, e.g.: function have_program (name, code, cmd, tmp) { cmd = "which " name code = (cmd | getline tmp) close(cmd) return code; } By the way, note the extra unused function arguments to provide local variables. "code" contains the return code from "getline", not the return code from "which". When I read this I thought you were looking for the exit code from "which", but apparently all you actually want is it's output, which is an easier task. Given that, either save the output in a file then read it back, e.g.: system("command > file") getline result < "file" or use getline from a pipe as you discovered, e.g.: "command" | getline result close("command") or use coprocesses, e.g.: print "input" |& "command" "command" |& getline result Just beware of using getline as it has a ton of caveats (including the one that led me to use "tmp" as an argument). You really should get the Gawk book (http://www.oreilly.com/catalog/awkprog3/) if you're going to be using awk a lot. Regards, Ed. |
|