How can I pass an awk variable to a bash command run within awk?
I'm using awk to calculate a running sum of threads for each of a certain process, but then I want to use each pid to access the /proc/$pid filesystem.
The line marked broken below is incorrect, because $pid is non-existent.
How can I export the awk variable pid to the shell command I get awk to run?
(Line breaks added for readability)
$ ps -ALf | grep $PROC | grep -v grep | \ # get each pid
awk '{threads[$2]+=1} END \ # count num threads per pid
{ \
for (pid in threads) \
"cat /proc/$pid/cwd"|getline cwd; \ # get 'cwd' (**broken**)
print cwd ": " threads[pid] \ # display 'cwd' and num threads
}'
You cannot cat /proc/$pid/cwd it is a symlink to a directory. One way to resolve symlinks is with readlink from coreutils.
Here is a working example using bits from TrueY and Barmer:
ps -C $PROC |
awk '{ t[$1] } END { for(p in t) { "readlink -f /proc/" p "/cwd" | getline c; print c } }'
A couple of things to notice:
t[$1]).You can do the whole lot in awk without any greps link this:
% ps -ALf | awk -v proc="${PROC}" '
$0 ~ proc && !/awk/ {
threads[$2] += 1
}
END {
for (pid in threads) {
"readlink /proc/" pid "/cwd" | getline dir
printf "%d: %d %s\n", pid, threads[pid], dir
dir=""
}
}'
A few things worth noting:
-v proc="${PROC}" assigns the environment variable ${PROC} to an awk variable, proc"readlink /proc/" pid "/cwd" concatenates the three strings. There's no need for any concatenation operator in awk.dir="" resets the dir variable, in case the symlink is not readable the next time around the loop.If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With