Quantcast
Channel: How to print own script name in mawk? - Unix & Linux Stack Exchange
Viewing all articles
Browse latest Browse all 6

Answer by Thor for How to print own script name in mawk?

$
0
0

I don't know any direct way of getting the command name from within awk. You can however find it through a sub-shell.

gawk

With GNU awk and the ps command you can use the process ID from PROCINFO["PID"] to retrieve the command name as a workaround. For example:

cmdname.awk

#!/usr/bin/gawk -f

BEGIN {
  ("ps -p " PROCINFO["pid"] " -o comm=") | getline CMDNAME
  print CMDNAME
}

mawk and nawk

You can use the same approach, but derive awk's PID from the $PPID special shell variable (PID of the parent):

cmdname.awk

#!/usr/bin/mawk -f

BEGIN { 
  ("ps -p $PPID -o comm=") | getline CMDNAME
  print CMDNAME
}

Testing

Run the script like this:

./cmdname.awk

Output in both cases:

cmdname.awk

Viewing all articles
Browse latest Browse all 6

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>