Skip to main content
deleted 119 characters in body
Source Link
Zombo
  • 1
  • 7
  • 47
  • 65

Perl and Ruby exit actually exits:

$ perl -e 'BEGIN {print 1; exit}; END {print 2}'
1

$ ruby -e 'BEGIN {print 1; exit}; END {print 2}'
1

Not so for Awk:

$ awk 'BEGIN {print 1; exit}; END {print 2}'
1
2

I tried to solve this with a helper script:"help.awk"

$ cat helper.awk
function really_exit() {
  IM_SURE = 1
  exit
}
END {
  if (IM_SURE) {
    exit
  }
}
function really_exit() {
  IM_SURE = 1
  exit
}
END {
  if (IM_SURE) {
    exit
  }
}

but this creates its own problem. If my "normal" script"prog.awk" has only the begin block block:

$ cat normal.awk
BEGIN {
  print "test script"
  print "script should exit after this prints"
}
BEGIN {
  print "start"
}

Then running it:

awk -f helperhelp.awk -f normalprog.awk

will cause it to hang waiting for input. Is a better solution available, for really exiting an Awk script?

Perl and Ruby exit actually exits:

$ perl -e 'BEGIN {print 1; exit}; END {print 2}'
1

$ ruby -e 'BEGIN {print 1; exit}; END {print 2}'
1

Not so for Awk:

$ awk 'BEGIN {print 1; exit}; END {print 2}'
1
2

I tried to solve this with a helper script:

$ cat helper.awk
function really_exit() {
  IM_SURE = 1
  exit
}
END {
  if (IM_SURE) {
    exit
  }
}

but this creates its own problem. If my "normal" script has only the begin block:

$ cat normal.awk
BEGIN {
  print "test script"
  print "script should exit after this prints"
}

Then running it:

awk -f helper.awk -f normal.awk

will cause it to hang waiting for input. Is a better solution available, for really exiting an Awk script?

Perl and Ruby exit actually exits:

$ perl -e 'BEGIN {print 1; exit}; END {print 2}'
1

$ ruby -e 'BEGIN {print 1; exit}; END {print 2}'
1

Not so for Awk:

$ awk 'BEGIN {print 1; exit}; END {print 2}'
1
2

I tried to solve this with a "help.awk"

function really_exit() {
  IM_SURE = 1
  exit
}
END {
  if (IM_SURE) {
    exit
  }
}

but this creates its own problem. If "prog.awk" has only the begin block:

BEGIN {
  print "start"
}

Then running it:

awk -f help.awk -f prog.awk

will cause it to hang waiting for input. Is a better solution available, for really exiting an Awk script?

Tweeted twitter.com/StackUnix/status/980051016489021441
Source Link
Zombo
  • 1
  • 7
  • 47
  • 65

Really exit Awk script

Perl and Ruby exit actually exits:

$ perl -e 'BEGIN {print 1; exit}; END {print 2}'
1

$ ruby -e 'BEGIN {print 1; exit}; END {print 2}'
1

Not so for Awk:

$ awk 'BEGIN {print 1; exit}; END {print 2}'
1
2

I tried to solve this with a helper script:

$ cat helper.awk
function really_exit() {
  IM_SURE = 1
  exit
}
END {
  if (IM_SURE) {
    exit
  }
}

but this creates its own problem. If my "normal" script has only the begin block:

$ cat normal.awk
BEGIN {
  print "test script"
  print "script should exit after this prints"
}

Then running it:

awk -f helper.awk -f normal.awk

will cause it to hang waiting for input. Is a better solution available, for really exiting an Awk script?