All,
I'm passing variables to AWK using the -v option (as recommended in the man page).
This seems to work, but not when the variable is placed inside a regexp for matching.
This is my script's input:
USER PID %CPU %MEM VSZ RSS TTY STAT START TIME COMMAND
root 3098 0.0 0.0 1388 444 tty5 S Jul08 0:00 /sbin/mingetty tty5
root 3099 0.0 0.0 1388 444 tty6 S Jul08 0:00 /sbin/mingetty tty6
myuser1 3332 0.0 0.4 50196 17716 ? S Sep03 0:00 rvrd -listen tcp:localhost:xxxx -http xxxx -no-permanent -reliability
myuser1 3747 0.0 0.8 100048 35448 ? S Sep03 0:00 rvrd -listen tcp:localhost:xxxx -http xxxx -permanent -reliability 30
AWK is being called from a bash shell script:
#Username is like 'root'
$NAWK -v ScriptPid="$$" -v UserName="$USERNAME" '{
print "Looking for:", UserName "
#This condition never matches for some reason:
if ( ($1 ~ /UserName/) && ($2 !~ /^[ ]*ScriptPid[ ]*$/ ) {
FoundProcess = "YES"
print "A process is still running as", UserName ":", $1 ",", $2 ",", $11 "\n"
}
else {
print "Not true for this line:"
print $0
}
}
END {
if( FoundProcess != "YES" ) {
exit 0
}
else {
exit 1
}
}' << EOF
`/path/to/ps`
EOF
Unfortunately, the code never matches the if statement.
Does anyone know how I can solve this one please?
Thanks in advance,
James.
I'm passing variables to AWK using the -v option (as recommended in the man page).
This seems to work, but not when the variable is placed inside a regexp for matching.
This is my script's input:
USER PID %CPU %MEM VSZ RSS TTY STAT START TIME COMMAND
root 3098 0.0 0.0 1388 444 tty5 S Jul08 0:00 /sbin/mingetty tty5
root 3099 0.0 0.0 1388 444 tty6 S Jul08 0:00 /sbin/mingetty tty6
myuser1 3332 0.0 0.4 50196 17716 ? S Sep03 0:00 rvrd -listen tcp:localhost:xxxx -http xxxx -no-permanent -reliability
myuser1 3747 0.0 0.8 100048 35448 ? S Sep03 0:00 rvrd -listen tcp:localhost:xxxx -http xxxx -permanent -reliability 30
AWK is being called from a bash shell script:
#Username is like 'root'
$NAWK -v ScriptPid="$$" -v UserName="$USERNAME" '{
print "Looking for:", UserName "
#This condition never matches for some reason:
if ( ($1 ~ /UserName/) && ($2 !~ /^[ ]*ScriptPid[ ]*$/ ) {
FoundProcess = "YES"
print "A process is still running as", UserName ":", $1 ",", $2 ",", $11 "\n"
}
else {
print "Not true for this line:"
print $0
}
}
END {
if( FoundProcess != "YES" ) {
exit 0
}
else {
exit 1
}
}' << EOF
`/path/to/ps`
EOF
Unfortunately, the code never matches the if statement.
Does anyone know how I can solve this one please?
Thanks in advance,
James.