ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
According the The GNU Awk User's Guide "the value of ARGV[0] (see Built-in Variables) varies depending upon your operating system. Some systems put ‘awk’ there, some put the full pathname of awk (such as /bin/awk), and some put the name of your script (‘advice’). Don't rely on the value of ARGV[0] to provide your script name".
So is there any way for an executing awk script find its own name? AFAIK it has no way to determine its own PID so it can't use the process table via ps ...
Despite scouring the awk man page for an equivalent of bash' $$ I had not found PROCINFO["pid"] so that was useful to learn.
I could not get the ps solution to work because the COMM field was not wide enough for my script's name and attempts to widen the column were not effective (-o comm:<big number>, -w option). Using cmd instead of comm worked but gave a space-separated list which would break if there was a space in the pathname so I chose the /proc method (which will (?) break for NULs in the pathname but that's acceptable).
Here's the final solution, adapting drunna's method to my verbose style that puts clarity above conciseness:
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.