Results 1 to 3 of 3
Hey guys,
I have been given a task at work of porting a script that is written for OpenBSD and I got to this block of code and can't for ...
- 01-13-2008 #1Just Joined!
- Join Date
- Jan 2008
- Posts
- 1
Help Understanding Block of Code
Hey guys,
I have been given a task at work of porting a script that is written for OpenBSD and I got to this block of code and can't for the life of me understand what its doing.
This is the bit that is tricking me, is this like a for loop or something?
This is the full statement that is getting me.Code:echo ${IP1} | { IFS='.' read ap1 ap2 ap3 ap4
I know roughly what it is doing, but the first part of the statement is throwing me out. can someone please help me understand?Code:echo ${IP1} | { IFS='.' read ap1 ap2 ap3 ap4 echo ${IP2} | { IFS='.' read bp1 bp2 bp3 bp4 BCXDIFF=0 if [ "${bp4}" -gt "${ap4}" ] ; then BCXDIFF=$((${BCXDIFF}+256-${bp4}+${ap4})) ap3=$((${ap3}-1)) else BCXDIFF=$(($BCXDIFF+${ap4}-${bp4})) fi if [ "${bp3}" -gt "${ap3}" ] ; then BCXDIFF=$((${BCXDIFF}+((256-${bp3}+${ap3})*256))) ap2=$((${ap2}-1)) else BCXDIFF=$((${BCXDIFF}+((${ap3}-${bp3})*256))) fi if [ "${bp2}" -gt "${ap2}" ] ; then echo BCXDIFF=$((${BCXDIFF}+((256-${bp2}+${ap2})*256*256))) ap1=$((${ap1}-1)) else BCXDIFF=$((${BCXDIFF}+((${ap2}-${bp2})*256*256))) fi if [ "${bp1}" -gt "${ap1}" ] ; then return 1 else BCXDIFF=$((${BCXDIFF}+((${ap1}-${bp1})*256*256*256))) echo ${BCXDIFF} return 0 fi } }
Thanks,
nathan
- 01-13-2008 #2
What shell was that written for? AFAIK, OpenBSD does not install bash by default.
IFS is a bash internal that refers to 'internal field separator'. But, again, I'm not convinced that was even written for bash.
Presumably the author's intent was to take a string (e.g. an IP address), stuff it into a variable (IP1), and then let bash separate the octets out by specifying an IFS of ".".
That code snippet does not work for me, BTW. Personally I'd have awk do the work the script is trying to do.
- 01-13-2008 #3Linux Enthusiast
- Join Date
- Apr 2004
- Location
- UK
- Posts
- 658
That's an interesting trick.
I'm guessing that $IP1 contains an IP address by this point in the script.
IFS is the word separator, usually space, tab and newline. By telling read to use the '.' char it will treat the different octets of the address as separate fields and read them into the ap1 to ap4 variables.
Comapre:
withCode:echo 1 2 3 4 | { read a b c d ; echo $a $b $c $d ; }
You should get the same output both times. Because IFS= and read are on the same line (or up to the semicolon in this case) it only affects that single command.Code:echo 1.2.3.4 | { IFS='.' read a b c d ; echo $a $b $c $d ; }
It's not looping, but the curly braces group the contained commands together otherwise $a, $b, $c and $d would be discarded before the echo was executed.
More information can be found on this shell programming page.
Let us know how you get on,
Chris...To be good, you must first be bad. "Newbie" is a rank, not a slight.


Reply With Quote