Results 1 to 6 of 6
hi.
i have been working on this whole day and did not find solution... I am trying to grep IP address from a file /opt/OpenIMSCore/etc/icscf.cfg which is in string "listen=222.222.222.222" ...
- 01-27-2011 #1Just Joined!
- Join Date
- Jan 2011
- Posts
- 4
BASH: strange string behaviour
hi.
i have been working on this whole day and did not find solution... I am trying to grep IP address from a file /opt/OpenIMSCore/etc/icscf.cfg which is in string "listen=222.222.222.222" later, i want to check if it is really an IP address and nothing else, so i want to do comparison whether the numbers are from 0-255 range. i am using following formula
curIP=`grep listen /opt/OpenIMSCore/etc/icscf.cfg | awk -F"=" '{ print $2 }'|egrep '^[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}'`;
if [ -z $curIP ]; then echo "incorrect IP address in /opt/OpenIMSCore/etc/icscf.cfg"; exit 0; fi
the output seems to be OK, but if i want to compare the values to be from range 0-255 with this condition
if [[ (${curIP[0]} -le 255) && (${curIP[1]} -le 255) && (${curIP[2]} -le 255) && (${curIP[3]} -le 255) ]]; then
echo OK
fi
the last part of the IP address makes problem - it is probably somehow strangely ended so i cannot use it in the condition (however this one works fine: if [[ (${curIP[0]} -le 255) && (${curIP[1]} -le 255) && (${curIP[2]} -le 255) ]]; )
so, definitelly, there is something wrong with the end of curIP variable and i do not not how to deal with it - this way i cannot work with the variable.
other result of research: i tried many combinations of brakets, [, {, $... but none worked even they should have had according to forums.
than i tried some console outputs like echo $curIP"sme"; but result is not "222.222.222.222sme" but: sme.255.255.255 - if i add anything to the string, it starts to put it to the beginning of the line!
one more note: variables ${curIP[0]} ${curIP[1]} ${curIP[2]} work just fine.
could you please help me to find out, how to get usefull output from the file?
- 01-27-2011 #2Just Joined!
- Join Date
- Jan 2011
- Posts
- 4
i also found out, that if i try
root@openims:~# e=`grep root /etc/passwd |awk -F":" '{ print $7 };'` && echo $e"sme"
the output is correct:
/bin/bashsme
but if i try it on my file (i put the root line to the file to try out this)
e=`grep root /opt/OpenIMSCore/etc/icscf.cfg | awk -F":" '{ print $7 }'` && echo $e"sme"
the output is
smen/bash
info about both files:
root@openims:~# ls -al /etc/passwd
-rw-r--r-- 1 root root 1601 2010-11-05 19:14 /etc/passwd
root@openims:~# ls -al /opt/OpenIMSCore/etc/icscf.cfg
-rwxrwxrwx 1 root root 8066 2011-01-27 18:52 /opt/OpenIMSCore/etc/icscf.cfg
i realy do not understand this behaviour
- 01-27-2011 #3Just Joined!
- Join Date
- Mar 2007
- Location
- Bogotá, Colombia
- Posts
- 39
The problem is with your regexp, try this:
grep -E "^((2[0-4][0-5])|(1?[0-9]?[0-9]))\.((2[0-4][0-5])|(1?[0-9]?[0-9]))\.((2[0-4][0-5])|(1?[0-9]?[0-9]))\.((2[0-4][0-5])|(1?[0-9]?[0-9]))"
that should look out for IPs
- 01-27-2011 #4Just Joined!
- Join Date
- Jan 2011
- Posts
- 4
unfortunately, it's not the case. the
grep -E "^((2[0-4][0-5])|(1?[0-9]?[0-9]))\.((2[0-4][0-5])|(1?[0-9]?[0-9]))\.((2[0-4][0-5])|(1?[0-9]?[0-9]))\.((2[0-4][0-5])|(1?[0-9]?[0-9]))" /opt/OpenIMSCore/etc/icscf.cfg
gives me no output
- 01-27-2011 #5Just Joined!
- Join Date
- Mar 2007
- Location
- Bogotá, Colombia
- Posts
- 39
My bad.
The regexp I sent checks for lines that start with a valid IP
change de '^' in the regexp with the "listen=" you need to look for. That should do the trick
- 01-28-2011 #6Just Joined!
- Join Date
- Jan 2011
- Posts
- 4
dear all.
i don't have any idea why, but it works now.
curIP=`grep listen /opt/OpenIMSCore/etc/icscf.cfg | awk -F"=" '{ print $2 }' |egrep '^[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}'`;
OIFS=$IFS
IFS='.'
curIP=($curIP)
IFS=OIFS
if [[ (${curIP[0]} -gt 255) && (${curIP[1]} -gt 255) && (${curIP[2]} -gt 255) && (${curIP[3]} -gt 255)]]; then echo "incorrect IP address in /opt/OpenIMSCore/etc/icscf.cfg; Exiting."; fi
maybe reboot helped, but really dont know. i openned the /opt/OpenIMSCore/etc/icscf.cfg in odl vi and there were ^M characters at the end of lines.


Reply With Quote