Timeline for answer to How to print own script name in mawk? by Ed Morton
Current License: CC BY-SA 3.0
Post Revisions
14 events
| when toggle format | what | by | license | comment | |
|---|---|---|---|---|---|
| Jun 15, 2023 at 9:09 | comment | added | Olivier Dulac |
instead of -v cmd="$0", you could do a more complex one to : 1) take into account non-gnu environnements (but I still assume you use bash... ymmv), 2) find if possible the name of a sourced script, and whether the scirpt is invoked with a full path, from $PATH, etc : with : truename=${BASH_SOURCE[0]:-$0}; name="$( basename "$truename" )"; dir="$( cd -P "$( dirname "$truename" )" && pwd)"; awk -v cmd="${dir}/${name}" '....' "$@"
|
|
| Oct 11, 2017 at 15:43 | comment | added | Adam Katz |
Good point, @Ed. Verified as failing in dash (which returns the previous command (or else the shell itself) rather than the current one). ksh93 interestingly prefixes the PID in asterisks, e.g. *12345*/tmp/test.awk. ARGV[0] is reliably always awk in dash, bash, zsh, and ksh93.
|
|
| Oct 10, 2017 at 18:08 | comment | added | Ed Morton |
Right but the portability question isn't "is ENVIRON[] portable" it's "does ENVIRON["_"] produce the calling shell script path when printed from every awk called via a shebang from every shell"? I would never call an awk script from a shebang to I personally don't care about the answer but just thought I'd mention it.... Oh I see in the comments above that @cuonglm answered that it's only supported in some shells.
|
|
| Oct 10, 2017 at 17:30 | comment | added | Adam Katz |
ENVIRON is very portable. It is a special variable defined in the POSIX standard itself.
|
|
| Jan 11, 2017 at 12:06 | history | edited | Jeff Schaller♦ | CC BY-SA 3.0 |
added 9 characters in body
|
| Sep 9, 2015 at 19:45 | comment | added | Barmar |
Calling your script tst.sh is misleading. It's an awk script, not a shell script. BEGIN is not a valid shell command.
|
|
| Sep 8, 2015 at 16:16 | comment | added | cuonglm | @cipper: Don't rely on it, as I said in my comment above, it only work with bash, zsh, or ksh variants. | |
| Sep 8, 2015 at 16:04 | comment | added | cipper |
Thank you! ENVIRON["_"] works perfectly, and it doesn't call any external program. The second option awk -v ... depends on how one runs the script; I don't want this.
|
|
| Sep 8, 2015 at 16:01 | vote | accept | cipper | ||
| Sep 8, 2015 at 15:35 | comment | added | cuonglm | Let @cipper clarify. | |
| Sep 8, 2015 at 15:13 | comment | added | Ed Morton |
The OP simply wants to print the name of a shell script which he has very unfortunately named myscript.awk.
|
|
| Sep 8, 2015 at 14:27 | comment | added | cuonglm | Note that the first one only work in bash, zsh or ksh. The later is about shell script, not awk script. | |
| Sep 8, 2015 at 14:21 | history | edited | Ed Morton | CC BY-SA 3.0 |
added 73 characters in body
|
| Sep 8, 2015 at 14:16 | history | answered | Ed Morton | CC BY-SA 3.0 |