last day (15 days later) » 

11:06 PM
1
A: AWK script shebang to allow dash-prefixed arguments

kvantourUnderstanding the problem: As far as I understand, the OP has a complex script called script.awk: #!/usr/bin/awk -f BEGIN{print "ARGC", ARGC; for(i=0;i<ARGC;++i) print "ARG"i,ARGV[i]} which the OP would like to call using various traditional POSIX-style one letter options, or GNU-style long ...

 
For me, this solution prints Hangup at the end of the script's execution (could be WSL Debian's sh quirk to report SIGHUP). It seems to me that replacing the kill command with plain old exit solves that. Am I missing something? Also, I believe you've omitted the key part required for dash-prefixed arguments to work (the AWK call should be awk -f "$0" "--" "$@" unless I'm mistaken).
As far as I can tell { awk -f "$0" "--" "$@"; exit; } works just fine. Thanks a ton, this is a brilliant solution! Please edit your answer to contain the full solution to the question, so that I can accept it.
 
You cannot use the exit statement as Awk will read the first line and immediately terminate due to the exit and then process the END statement.
 
You are right, good point. The files passed to the script are indeed not processed. However both BEGIN and END are executed, which is good enough for me. I didn't realize because my BEGIN rule ends with exit anyways. Still, you might want to add the missing "--" to the awk call in your answer because the current code behaves much like regular #!/usr/bin/awk -f.
@EdMorton I must have all the code in a single executable file. If I pass the code to awk from a shell script, I will have to have two copies of the code in order to get AWK syntax highlighting (AWK code inside of a shell string isn't typically highlighted properly). This makes it extremely easy to make a mistake when copying the code or to forget to sync any changes, basically a DRY violation.
@EdMorton This has nothing to do with my editor. It's a general problem with any editor. Having the AWK code inside of another language's code is simply inconvenient, impractical and an awful practice, which could easily lead to tons of unforeseen problems. I'm looking for a solution to a specific problem. If I wanted to embed my AWK code into a shell code, I could have done that easily, but I wanted to execute the AWK code without having to worry it's gonna be unexpectedly manipulated by another program.
@glennjackman Makes sense since -- is AWK's decrement operator, but I've already put it into quotes in my first comment. However, the first note about exec is very interesting. It indeed seems to do the trick without having to use kill or exit. By the way, as far as quotation marks go, { exec awk -f "$0" "--" "$@"; } should be enough for as long as exec, awk and f aren't defined within the AWK script (which should be easy to avoid). Thanks for that note.
 
@natiiix I have massively updated the answer coming to the conclusion that it does not really work.
@EdMorton I updated my answer. I would appriciate it if you could have a look at it if I did not make an oversight.
 
@kvantour Do you delete the ARGVs? Because it works fine for me when I do.
 
11:06 PM
@glennjackman The double hyphen method seems to expect that all arguments after are files. So if you have a -- -arg in it, it will try to process the file -arg for the first action pattern pair. Since this file does not exist, awk will actually crash.
 
I think your code works just fine, if you delete ARGV[key] after your somehow process it (unless it's an actual file, which is supposed to be handled by AWK rules and not code in BEGIN).
 
@natiiix I am unable to get anything running on my side. If I take my example code, and pass it -arg file it will complain that -arg is not a file. This is an effect of the -- option which assumes that all subsequent arguments are files.
If you do a delete of ARGV[key] you could make it work. That is true, but that only depends on how you process the argument list.
however, be aware that you ask for a posix solution, but -- is not posix, neither is mawk
 
Well, if you keep the args in ARGV and not modify ARGC, then AWK will iterate through them and treat them as files. I believe that deleting ARGVs is the "right" solution to parsing arbitrary arguments.
Oh, damn, is that so? Would POSIX awk implementation be unable to take arbitrary arguments then?
 
you are correct, argument processing makes best sense this way.
I do not know if it can. awk is not realy designed to have flags as extra arguments. That is why they allow the usage of assignments in the command line.
such as awk -f script.awk n=1 m=2 p="foobar" file
 
Yes, that's what I used before. I parsed the arguments using shell and then used -v to pass them to AWK. It seems mawk and gawk make up a vast majority of the market, so while not exactly POSIX, it should work on most platforms.
Thanks for your input.
 

last day (15 days later) »