I can't seem to pass variables properly into a nawk statement

Ok, So up front I'm going to say that I'm a very elementary scripter, and I tend to use tools I don't fully understand, but I shotgun at something until I can get it to work...that said, I can't for the life of me understand why I can't get this to go down the way I want it to.

The goal:
-to establish todays date
-to take a look at a log file (in this case the /var/adm/messages log)
-to check if the last line of the log matches todays date and if so ... (eventually it will be to dump all lines of the log with todays date to a text file that will at some point be picked up by the notification server and go out in an email)

My issue is that though I can establish the date, and create variables based upon it, I can't get nawk to accept the variables I've created.

What I have so far:

#!/usr/bin/bash
#collect todays date (date)
d=`date|awk '{print $3}'`
#collect todays date (month)
m=`date|awk '{print $2}'`
#collect errors for todays date (day and month)
e=`grep ""$d" "$m"" /var/adm/messages`
tail -1 /var/adm/messages|nawk -v m=$m -v d=$d  '{if ($1 == "$m" && $2 == "$d") print "success"}'
#if 
#        [ "$z" = "success" ]; 
#then echo "$e"
#fi

this returns nothing, no errors, just goes straight back to the prompt.

Now as a matter of checking my work I've tried replacing that last nawk statement with

tail -1 /var/adm/messages|nawk -v m=$m  '{print $m}'

I figured this would at least tell me if my variable is making it into the nawk statement, but this returns:

"nawk: illegal field $(Mar)
input record number 1
source line number 1"

the last thing that I've tried, just to make sure my syntax isn't completely foobar was

tail -1 /var/adm/messages|nawk -v m=$m -v d=$d  '{if ($1 == "Mar" && $2 == "30") print "success"}'

and that returns success...

So from what I can gather, my overall nawk statement is good, it's just not using the variables from the rest of the script.

So any input would be appreciated. Also (although, I want to learn this way) if anyone has any insight on a better method of keeping track of the messages log (or any log) please chime in.

In awk, $m means the field number indicated by the number in the variable m. So no wonder that part didn't work.

Something tells me the values of your variables are not completely right. We saw that m equals "Mar" but what about n? I would say that what you have ought to work but I haven't looked that closely.

As an aside, date has options to output just the month name or the day within the month; see its manual page for details. That's also more portable because the default date output format depends on the user's locale, etc.

You are not using e for anything. I would advise against using backticks -- just redirect the output straight where you want it. But the nawk script looks like a promising start.

P.S. The log file is probably growing while you work so could it be that you got unlucky, and the last line didn't happen to have today's date when you tested that awk script?

Thanks for the input. I'm not sure what you mean by

"In awk, $m means the field number indicated by the number in the variable m. So no wonder that part didn't work."

I've checked and rechecked my variables and I'm relatively certain that they are correct. I know date ...print $3 gives me the day (for today the number 30) and I know date... print $2 gives me the Month (for today Mar). So for today for example, I know $d is 30 and $m is Mar.

So for example the last line of the /var/adm/messages currently is:

Mar 30 06:20:29 bms02-XXX-QN-XXXXX-XXXX inetd[164]: [ID 101010 daemon.error] ftp/tcp: bind: Address already in use

so by my understanding when I invoke

nawk -v m=$m -v d=$d  '{if ($1 == "$m" && $2 == "$d") print "success"}'
#if 

I'm saying
-within this statement make m the value of $m outside the statement
-within this statement make d the value of $d outside the statement
-if the first column of the last line of the log is equal to $m (Mar)
and
-the second column of the last line of the log is equal to $d (30)
then
-print "success"

but from what I'm gathering within the nawk statement, my variables aren't being set, so i'm basically getting

if column 1 = ""
and
if column 2 = ""
print "success"

but since that's not what column 1 & 2 are, it's failing and printing nothing

I hope that I'm explaining myself well here...I'm new to this forum, and I don't want to look silly...

Regarding $e, that's for future use...if I can get the nawk to print success, then once I uncomment those last few lines the bash if statement should make it go back through the log and print all the messages for that day (which as I said will eventually get dumped into an email so we can all panic over failed root logins)

Oh and yes the log is growing, but the last line always starts with Mar 30, so no matter what the line is, I should still be able to chop that off.

...Sorry for being so long winded, it's just my style

No, $d means $30 and $m means $Mar but you want 30 and Mar without the dollar signs.

Hi,

Given that AWK has a special way of passing variables into it, "$m" and "$d" are not recognized within AWK, it knows only m ( m="$m" ) and d ( d="$d" ).

nawk -v m=$m -v d=$d  '{if ($1 == "$m" && $2 == "$d") print "success"}'     # incorrect use of passing variables

So the correct use is:

nawk -v m=$m -v d=$d  '{if ($1 == m && $2 == d) print "success"}'     

That was it. I KNEW it was going to come down to me not understanding the syntax correctly. Thank you so much for your succinct answer.

era, you got there first, and rubin, you made it all clear. Again, thank you.

and once I uncommented (and remembered to add ;then) to my if statement, I got the log dump I was looking for.

Just out of curiosity, did I choose a completely convoluted solution to my issue (dumping only todays log) or was this at least remotely a intelligent method?

It's not convoluted at all, and you were very close already on the first try. Also, compliments for your ideas for how to debug the problem; again, you were so close, and it was just sheer bad luck you didn't figure it out completely on your own.