I’m trying to pass a variable to nawk in a bash script, but it’s not actually printing the $commentValue variable’s contents. Everything works great, except the last part of the printf statement. Thanks!
echo -n "Service Name: "
read serviceName
echo -n "Comment: "
read commentValue
for check in $(grep "CURRENT SERVICE STATE" $nagiosLog |grep -w "$serviceName" | nawk -F": " '{print $2}' |sort -u ) ; do
echo $check | nawk -F";" -v now=$now '{ printf( "[%u]=ACKNOWLEDGE_SVC_PROBLEM;"$1";"$2";2;1;0;admin;$commentValue"\n", now)}' >> $nagiosCommand
done
$commentValueis inside an invocation tonawk, so it is considered as a variable innawk, not a variable inbash. Since you do not have such a variable innawk, you won’t get anything there. You should first pass the variable “inside”nawkusing the-vswitch just like you did for thenowvariable; i.e.:Note the quotes – they are required in case
$commentValuecontains whitespace.