Le jeudi 7 juillet 2016 16:20:44 UTC+2, Janis Papanagnou a écrit :
> I'm wondering why you don't put both commands, print and close, into the
> if-block.
Yes, this would be version 3:
# Version 3:
BEGIN {
# Store DOS output (one line) from a program into an AWK variable:
cmd = "doff.exe yyyymmdd_hhmiss"
if ((cmd | getline awkvar) > 0) {
print awkvar
close(cmd)
}
}
But of course version 2 is best:
# Version 2:
BEGIN {
# Store DOS output (one line) from a program into an AWK variable:
cmd = "doff.exe yyyymmdd_hhmiss"
if ((cmd | getline awkvar) > 0) {
print awkvar
}
close(cmd)
}
To complete this thread, if the output consists of several lines, something like the following snipppet (part of a script of mine) can be used:
# Several lines:
BEGIN {
# Store DOS output (several lines) from a program into an AWK variable:
pdf_path = "c:\\test\\"
cmd = "dir " pdf_path "*.pdf /A:-D/b"
while ((cmd | getline awkvar) > 0) {
print "#" awkvar "#"
}
close(cmd)
}