View Single Post
Old Aug 17th, 2006, 12:36 AM   #1
a thing
Unverified User
 
a thing's Avatar
 
Join Date: Aug 2005
Location: none
Posts: 146
Rep Power: 0 a thing is on a distinguished road
$$i and rounding

I was looking back through the threads I started and came across http://programmingforums.org/forum/s...38&postcount=6. I thought that would be more accurate, so I've been trying to construct a function that prints the fastest domain from the list of domains given to it. So far I have:
#!/bin/sh

getmirror()
{
	declare -a averages #Make sure $averages doesn't get thrown away too soon by garbage collection.
	i=0
	for mirror; do
		averages[i]="$(ping -c 2 $mirror|tail -n1|cut -d/ -f5|cut -d. -f1)"
		i=$(expr $i + 1)
	done
	IFS="
" #So sort will count different elements of $averages as different values.
	bestaverage=$(echo "${averages[*]}"|sort -n|head -n1)
	size="${#averages[*]}"
	echo $bestaverage
	for (( i=0; "${averages[i]}" != "$bestaverage"; i++ )); do echo; done #The shell complains if there's nothing between do and ;done.
	echo $i
}

echo $(getmirror jaist.dl.sourceforge.net switch.dl.sourceforge.net easynews.dl.sourceforge.net)

My problems:
1. How would I reference the argument number $i? So if $i == 2, then reference $2. Like $$i, but that's not the correct syntax.
2. What's the best way to round a number? [ complains if there are decimals passed to it. cut -d. -f1 sort of works, but it isn't the most accurate.
__________________
Warning: My posts may change (dramatically) within the first 15 minutes they're posted.
Got 'Nux?—GNU/Linux and other free software support.
It's GNU/Linux, not just Linux.

Last edited by a thing; Aug 17th, 2006 at 12:50 AM.
a thing is offline   Reply With Quote