This is really frustrating because I can't figure it out.
I'm running a health check script. One of the items I'm checking is the amount of memory on a server. I use the free command, which outputs something like this (excerpt)
Mem: 100 100 100 100
Swap: 100 100 100 100
In my debugging I see that $1 is "Mem:" or "Swap:" (note the ":" does come out of `free`)
My if condition is if ($1 == "Swap:") then do something
On the screen the condition matches perfectly. But to awk it does not. It never falls into that condition. What am I doing wrong? How do I build my condition so that I can check $1 for "Swap:"? Is it the colon ":" that's throwing me off?
awk '{
printf "$1 $2 $3 $4 are " $1 " " $2 " " $3 " " $4 "\n" #(this is my debug statement)
if ($1 == "Swap:" && $3 != 0) # I comment out the && and it's the $1 failing for sure
printf "WARNING - SWAP IN USE \n"
else
some other checks which all work fine...
}' file.out >> health_check.out
cat health_check.out #another debug, actually I've got a mailx built here
Produces this:
$1 $2 $3 $4 are Mem: 1001 800 200
Output for falling through the if statement
$1 $2 $3 $4 are Swap: 2015 98 1917
same type stuff from above for falling through the else clause. Definitely not the " WARNING - SWAP IN USE " I was hoping for.
---------- Post updated at 02:17 PM ---------- Previous update was at 02:10 PM ----------
Excellent call on the od command. I forgot about that. It pans out as "Swap:", just like it's supposed to, but at least I know there's no special characters or anything else in there that might throw the condition expression off.
which is the last variable on the print line did some sort of carriage return on the same line and covered up my output with the rest of the print statement. When I changed it to $3 instead or any other variable the output was fine. $4 is just a numeric like the rest, but it screwed with my output. So I changed the verbiage a little to get the same output. Perhaps the last output on the line has an embedded <CR> in it. Hey, that gives me another check with Rudi's od!