Giter Site home page Giter Site logo

Comments (4)

benhoyt avatar benhoyt commented on May 27, 2024

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.

paulapatience avatar paulapatience commented on May 27, 2024

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.

benhoyt avatar benhoyt commented on May 27, 2024

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.

benhoyt avatar benhoyt commented on May 27, 2024

Thanks @paulapatience -- just tagged and released v1.20.0 with this feature.

from goawk.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo 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.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.