awk variable troubles
Hi all,
Still working on getting better with awk, tried to use some of the advice members had given on other threads related to this but am having trouble figuring out where I'm going wrong. Here's the code: I'll spare you from reading most of the code as I do know for a fact that my variable is being set correctly to an integer when I run it with sh -x. So currently my sh -x shows the LASTOCTET benig set as: LASTOCTET=64 Then this code is a few lines down: Code:
if [[ "$LASTOCTET" -lt 254 ]] Code:
+ LASTOCTET=65 What am I doing wrong here? It looks like im passing the varible into AWK correctly as I can see it being set to 65 so why is the $n not being printed? |
Awk's field variables are $1, $2 etc. Regular variables are referenced wthout '$'.
I have also become a fan of printing with printf. It shouldn't make much difference but formatting is easier: printf ("%d.%d.%d.%d\n", $1, $2, $3, n) $n happens to be $1, $2,... for n=1, 2... Incredible flexible language! Hence most likely you were referring to $65 which doesn't exist. jlinkels |
First off, in the bash portion of your script you use round brackets some times and other times square. As you are doing numerical comparisons I would suggest using round all the time to save the
confusion. Secondly, do you really need awk here?? So without seeing the rest and assuming data correct, it could look like: Code:
if (( LASTOCTET < 254 )) |
Quote:
I tried using round brackets but was having issues with the integer having a space appended to it so instead of worrying too much about it changed it to square to do a string comparison. I switched to using awk because I was going to have to manipulate the different octets of teh IP easily and I figured awk could split it and rejoin the IP easy enough. If it was just the last octet it would make it much easier. |
Quote:
Code:
if [[ "$LASTOCTET" -lt 254 ]] Code:
INTEGER1 -lt INTEGER2 Quote:
Code:
OCTETS=(${LASTIP//./ }) |
I know I've already suggested splitting the octets into an array in one of the previous threads, even including code for it.
|
Quote:
Thanks for the input and knowledge grail. |
Quote:
|
Like grail though, it doesn't make sense to me that the brackets used would be the cause for something like that. The extra space had to be added elsewhere, and was probably sitting there from before the test, perhaps even carried into it from the original input?
|
All times are GMT -5. The time now is 04:55 PM. |