Comments (4)
I'm open to adding this. However, I don't quite understand the usefulness. Why is it a problem that hyphen-prefixed options are processed by AWK rather than the script? Even the Gawk manual's Executable Scripts section shows a hash bang of #!/bin/awk -f
.
And I'm not sure I see what's wrong with the --
approach? Can you help me understand why this is really needed?
If I do add it, I'd prefer just one way to do it. Copying Gawk's -E
seems best to me (as GoAWK uses all short options right now).
from goawk.
Hopefully the following example is clear. Put the following contents into a file named awc-test.sh
:
#!/usr/bin/env bash
read -r -d '' awc <<EOF
BEGIN {
for (i = 1; i < ARGC; i++) {
if (ARGV[i] == "--") {
delete ARGV[i++]
break
}
else if (ARGV[i] !~ /^-./) break
else if (ARGV[i] == "-c") c = 1
else if (ARGV[i] == "-w") w = 1
else if (ARGV[i] == "-l") l = 1
else printf "awc: unknown option: %s\n", ARGV[i] >"/dev/stderr"
delete ARGV[i]
}
if (!c && !w && !l) c = w = l = 1
}
{ cs += length(); ws += NF; ls++ }
END { printf "%s%s%s\n", l?ls" ":"", w?ws" ":"", c?cs" ":"" }
EOF
printf "#!/usr/bin/env -S mawk -f\n%s\n" "$awc" >mawc-f
printf "#!/usr/bin/env -S gawk -f\n%s\n" "$awc" >gawc-f
printf "#!/usr/bin/env -S awk -Wexec\n%s\n" "$awc" >awc-e
chmod +x mawc-f gawc-f awc-e
run() { echo '$' "$@"; $@; }
run ./mawc-f awc-test.sh
run ./mawc-f -l awc-test.sh
run ./mawc-f -w awc-test.sh
run ./mawc-f -c awc-test.sh
run ./mawc-f -- awc-test.sh
run ./mawc-f -- -l awc-test.sh
run ./mawc-f -- -w awc-test.sh
run ./mawc-f -- -c awc-test.sh
run ./gawc-f awc-test.sh
run ./gawc-f -l awc-test.sh
run ./gawc-f -w awc-test.sh
run ./gawc-f -c awc-test.sh
run ./gawc-f -- awc-test.sh
run ./gawc-f -- -l awc-test.sh
run ./gawc-f -- -w awc-test.sh
run ./gawc-f -- -c awc-test.sh
run ./awc-e awc-test.sh
run ./awc-e -l awc-test.sh
run ./awc-e -w awc-test.sh
run ./awc-e -c awc-test.sh
run ./awc-e -- awc-test.sh
run ./awc-e -- -l awc-test.sh
run ./awc-e -- -w awc-test.sh
run ./awc-e -- -c awc-test.sh
and run it (mawk
and gawk
must be in PATH
, and awk
must refer to one of the two). The result is the following:
$ ./mawc-f awc-test.sh
55 239 1387
$ ./mawc-f -l awc-test.sh
mawk: not an option: -l
$ ./mawc-f -w awc-test.sh
mawk: not an option: -w
$ ./mawc-f -c awc-test.sh
mawk: not an option: -c
$ ./mawc-f -- awc-test.sh
55 239 1387
$ ./mawc-f -- -l awc-test.sh
55
$ ./mawc-f -- -w awc-test.sh
239
$ ./mawc-f -- -c awc-test.sh
1387
$ ./gawc-f awc-test.sh
55 239 1387
$ ./gawc-f -l awc-test.sh
gawk: fatal: cannot open shared library `awc-test.sh' for reading: No such file or directory
$ ./gawc-f -w awc-test.sh
239
$ ./gawc-f -c awc-test.sh
55 239 1387
$ ./gawc-f -- awc-test.sh
55 239 1387
$ ./gawc-f -- -l awc-test.sh
55
$ ./gawc-f -- -w awc-test.sh
239
$ ./gawc-f -- -c awc-test.sh
1387
$ ./awc-e awc-test.sh
55 239 1387
$ ./awc-e -l awc-test.sh
55
$ ./awc-e -w awc-test.sh
239
$ ./awc-e -c awc-test.sh
1387
$ ./awc-e -- awc-test.sh
55 239 1387
$ ./awc-e -- -l awc-test.sh
awk: ./awc-e:15: fatal: cannot open file `-l' for reading: No such file or directory
$ ./awc-e -- -w awc-test.sh
awk: ./awc-e:15: fatal: cannot open file `-w' for reading: No such file or directory
$ ./awc-e -- -c awc-test.sh
awk: ./awc-e:15: fatal: cannot open file `-c' for reading: No such file or directory
Without -Wexec
, it is impossible to mimic the command-line interface of wc
, or to write a script that behaves like a conventional command-line utility. --
is required to stop the Awk interpreter from parsing options, but then supposing you wanted to stop processing options in the script itself, you'd need a second --
. Using awk -f
in the hashbang line leaks the fact that the script is written in Awk. If you wanted to rewrite the script in another language (e.g., Go), the command-line interface would have to change (or remain unnatural).
As for providing -E
only, your reasoning makes sense. It would be trivial to update by Makefile an Awk script's hashbang line to use -E
rather than -Wexec
. At least it wouldn't require a wrapper shell script.
Note that providing options to the Awk interpreter is always possible even with -E
by explicitly invoking awk -f
. So nothing is lost by using -E
in the hashbang line.
from goawk.
Thanks for the additional justification and examples. I understand this now and think it makes sense. I've added this in #140 and will merge and tag a release in the next few days -- let me know what you think!
from goawk.
Thanks @paulapatience -- just tagged and released v1.20.0
with this feature.
from goawk.
Related Issues (20)
- Add Gawk built-in functions such as mktime and strftime HOT 5
- Should the output of `-d` option be 100% correct AWK code equivalent to the source? HOT 2
- Dynamically changing RS HOT 4
- Code coverage for GoAWK HOT 10
- Semicolon not allowed as terminator for normal pattern without action HOT 2
- Add logfmt support HOT 1
- Add JSON Lines support HOT 2
- Add some tests for the -d, -da, and -dt options
- Add optimization for a[1] -> a["1"]
- Should coverage profiles use relative filenames? HOT 2
- Deleting from array while looping HOT 2
- [Feature Request] Support case when end range pattern is not distinct from start pattern HOT 2
- Error parsing "cond && var=value" HOT 3
- Error parsing $++a
- Add support for 'nextfile'
- Add support for --csv (which Gawk and onetrueawk are adding)
- Wrong order of output with pipe and print HOT 1
- GoAWK 1.23.X fails on gron.awk HOT 7
- Mutually-recursive functions without type info cause compiler panic HOT 6
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from goawk.