Linux - NewbieThis Linux forum is for members that are new to Linux.
Just starting out and have a question?
If it is not in the man pages or the how-to's this is the place!
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
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:
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?
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 ))
then
(( LASTOCTET++ ))
TMPIP="${LASTIP%.*}.$LASTOCTET"
echo -e "$VM\t$TMPIP" >> /tmp/test1
elif (( LASTOCTET >= 255 ))
then
TMPIP="${LASTIP%.*}.0"
echo -e "$VM\t$TMPIP" >> /tmp/test1
fi
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 ))
then
(( LASTOCTET++ ))
TMPIP="${LASTIP%.*}.$LASTOCTET"
echo -e "$VM\t$TMPIP" >> /tmp/test1
elif (( LASTOCTET >= 255 ))
then
TMPIP="${LASTIP%.*}.0"
echo -e "$VM\t$TMPIP" >> /tmp/test1
fi
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.
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.
This doesn't really make much sense
Code:
if [[ "$LASTOCTET" -lt 254 ]]
By using -lt you are doing a numeric comparison and not a string comparison: (below is from man test)
Code:
INTEGER1 -lt INTEGER2
INTEGER1 is less than INTEGER2
Quote:
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.
So if it is manipulating octects you wish to do, bash is still very capable:
Code:
OCTETS=(${LASTIP//./ })
Now you can simply call each element of the OCTETS array for whichever piece you wish
By using -lt you are doing a numeric comparison and not a string comparison: (below is from man test)
Code:
INTEGER1 -lt INTEGER2
INTEGER1 is less than INTEGER2
I'm not sure why but I was receiving an invalid string error which showed an additional space being appended to the integer it was comparing to when using round brackets. I don't usually have much time to figure out why on most of these things, trying to administrate an extremely large environment by myself and since I transitioned from being a Linux admin to a backup admin I basically stopped scripting and now am re-learning a lot of what I used to know a year or two ago.
I know I've already suggested splitting the octets into an array in one of the previous threads, even including code for it.
Yes you have, I attempted to put in place the code you gave but I am also scripting against a proprietary database that is absolutely nothing like any sort of SQL and its formatting its garbage. I wasn't able to get it to work 100% correctly.
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?
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.