-1

I have a shell script use awk from https://devconnected.com/monitoring-linux-processes-using-prometheus-and-grafana/.

#!/bin/bash
z=$(ps aux)
while read -r z
do
   var=$var$(awk '{print "cpu_usage{process=\""$11"\", pid=\""$2"\"}", $3z}');
done <<< "$z"
echo $var

I know the $3 means the third matched string split by space. What is the $3z mean? Is z the param of read -r z? when I substitute $3z to $3, it changed nothing, why?

Renshaw
  • 1,075
  • 6
  • 12
  • probably a typo, in string context uninitialized variable will be considered as empty string.. value of `$3` and `z` is being concatenated.. `read -r z` and `z` in the `awk` code are unrelated – Sundeep May 14 '21 at 07:23
  • I think it is not a typo, because the shell script source https://devconnected.com/monitoring-linux-processes-using-prometheus-and-grafana/ tell me to change $3z to $4z. – Renshaw May 14 '21 at 07:29
  • See https://stackoverflow.com/questions/19075671/how-to-use-shell-variables-in-an-awk-script – Sundeep May 14 '21 at 07:40
  • thank you very much, the `z` behind `$3` is useless. – Renshaw May 14 '21 at 07:57

1 Answers1

0

Given that

  • awk has no concatenation operator -- strings are concatenated by placing them side-by-side with optional whitespace in between
  • awk variable names cannot begin with a number
  • unset variables are substituted with the empty string in "string context" and with the value 0 in "numeric context"

This is concatenating the value of the 3rd field with the contents of the z variable.

Demo:

$ echo foo1 foo2 foo3 | awk 'BEGIN {z = "bar"} {print $3z}'
foo3bar
glenn jackman
  • 238,783
  • 38
  • 220
  • 352