There are a few reasons why a print statement in awk may not succeed: no permissions to write to the specified file, file doesn't exist and awk can't create it, etc. How is it possible to test the success of an awk print statement? I've tried the following:
$ cat printError.awk
BEGIN {
if (! (print("Hello") > "/this/doesnt/exist")) {
print "Could not print"
}
}
But it gives a syntax error, I think because print is a statement and not a function.
$ awk -f printError.awk
awk: printError.awk:2: if (! (print("Hello") > "/this/doesnt/exist")) {
awk: printError.awk:2: ^ syntax error
awk: printError.awk:2: if (! (print("Hello") > "/this/doesnt/exist")) {
awk: printError.awk:2: ^ syntax error
awk: printError.awk:2: if (! (print("Hello") > "/this/doesnt/exist")) {
awk: printError.awk:2: ^ syntax error
EDIT: I found a solution for gawk 4.2+, but the environment I'm working in has only 4.0, so I'm still looking for a solution for that version.