1

I'm calling the sleep command from awk like so: system("sleep 15m") and found that when I interrupt that with CTRL-C it only interrupts the sleep command, not the script. So I tried wrapping the system function in an if statement, as suggested in this sample script and further explained here. It then looks like if (system("sleep 15m") != 0) exit 1.

That didn't work, so I tried print system("sleep 3"), which always returns 0, contrary to the stated behavior. Typing sleep 3 then echo $? in the shell produces 0 if I let it run and 130 if I interrupt it.

So what am I missing? I've checked for typos several times, tried the --posix and --traditional switches, and studied the user's guide, nothing changes it.

$ awk --version
GNU Awk 4.1.3, API: 1.1 (GNU MPFR 3.1.4, GNU MP 6.1.0)
Copyright (C) 1989, 1991-2015 Free Software Foundation.
  • Have you tried using kill or pkill to send specific signals to the sleep process directly, rather than relying on your terminal emulator's handling of Ctrl-C? That would at least eliminate one variable if it still returns 0 (or isolate the cause if it produces 130 or other). – Wildcard Aug 6 '18 at 21:20
  • print system("sleep 3") when killed should return 258 according to the docs, which it does on my system. It may depend on how your sh behaves, as system() is the equivalent of sh -c 'sleep 3' – jordanm Aug 6 '18 at 21:21
1

Appears to be a gawk bug

$ echo foo | gawk '{print system("sleep 3")}'
^C0
$ 

Seems related to Make bash exit != 0 when called by AWK and interrupted with ^C

gawk workaround, courtesy of Keith

$ echo foo | awk '{if(system("sleep 3 && exit 42")==42){print "completed"}else{print "interrupted"}}'
completed
$ echo foo | awk '{if(system("sleep 3 && exit 42")==42){print "completed"}else{print "interrupted"}}'
^Cinterrupted
$

nawk handles it correctly

$ echo foo | nawk '{print system("sleep 3")}'
^C0.0078125
$
  • 1
    Well, nawk on my system seems to be a symlink to gawk; but the related question details the problem quite nicely. I don't know why I couldn't find that one. – Keith Aug 6 '18 at 21:37
  • 1
    so if( system("sleep 3 && exit 42" ) != 42 ) exit 1 seems to work nicely as a workaround. – Keith Aug 6 '18 at 21:43
  • Good idea, have added your workaround to the main body of the answer. – steve Aug 6 '18 at 22:00

Not the answer you're looking for? Browse other questions tagged or ask your own question.