![]() |
|
|
|
|
|||||||
| Forums | Portal | Register | Forum Rules | FAQ | Contribute | Members List | Arcade | Search | Today's Posts | Mark Forums Read |
| Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts here. |
|
|
||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| how to access values of awk/nawk variables outside the awk/nawk block? | saniya | Shell Programming and Scripting | 5 | 05-13-2008 04:37 AM |
| Shell script not processing if statement properly | jonathan184 | Shell Programming and Scripting | 2 | 05-08-2007 12:24 PM |
| is it possible to pass external variable values to nawk? | swamymns | Shell Programming and Scripting | 1 | 02-02-2006 02:13 AM |
| nawk and variables | plimpix | Shell Programming and Scripting | 8 | 07-11-2005 08:56 AM |
| pf not working properly even with only "pass in all" and "pass out all" rules | xyyz | UNIX for Advanced & Expert Users | 4 | 12-30-2003 01:33 AM |
|
|
Submit Tools | LinkBack | Thread Tools | Display Modes |
|
|||
|
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: Code:
#!/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
Now as a matter of checking my work I've tried replacing that last nawk statement with Code:
tail -1 /var/adm/messages|nawk -v m=$m '{print $m}'
"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 Code:
tail -1 /var/adm/messages|nawk -v m=$m -v d=$d '{if ($1 == "Mar" && $2 == "30") print "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. |
| Forum Sponsor | ||
|
|
|
|||
|
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? Last edited by era; 03-30-2008 at 02:55 AM. Reason: Add P.S. |
|
|||
|
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 Code:
nawk -v m=$m -v d=$d '{if ($1 == "$m" && $2 == "$d") print "success"}'
#if
-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 |
|
|||
|
Quote:
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.
|
|||
| Google The UNIX and Linux Forums |