Programming Forums
User Name Password Register
 

RSS Feed
FORUM INDEX | TODAY'S POSTS | UNANSWERED THREADS | ADVANCED SEARCH

Reply
 
Thread Tools Display Modes
Old Aug 17th, 2006, 1: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 1:50 AM.
a thing is offline   Reply With Quote
Old Aug 23rd, 2006, 1:45 AM   #2
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
Kabump.
__________________
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.
a thing is offline   Reply With Quote
Old Aug 23rd, 2006, 9:08 AM   #3
DaWei
Resident Grouch
 
DaWei's Avatar
 
Join Date: Jun 2005
Posts: 6,453
Rep Power: 10 DaWei is on a distinguished road
1. Sorry, not familiar with bash.
2. Presuming you mean round to the nearest integer, add .5 to the number, then floor it (truncate the decimals).
__________________
Abstraction doesn't make it impossible to write bad code; it makes it possible to write superior code.
Contributor's Corner: Grumpy on C++ Exceptions DaWei on Pointers
DaWei is offline   Reply With Quote
Old Aug 23rd, 2006, 4:38 PM   #4
jim mcnamara
Hobbyist Programmer
 
Join Date: Jun 2005
Location: New Mexico
Posts: 228
Rep Power: 4 jim mcnamara is on a distinguished road
The print command line utility will round numbers.
z=2.111
printf "%.0f\n" $z

stick a :; between do and done That's a colon semicolon

Finally, I don't understand what you mean by reference $i. You want to get
the value of i outside the function? Or the value of the array element referenced by i - the array you declare inside the function?

Note: i only has scope during the "for" loop inside the getmirror function. The array "averages" only has scope inside the getmirror function.

They do not exist outside their scope.
jim mcnamara is offline   Reply With Quote
Old Aug 23rd, 2006, 10:57 PM   #5
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
Quote:
Originally Posted by jim mcnamara
The print command line utility will round numbers.
z=2.111
printf "%.0f\n" $z
Thanks, that worked. Now I have
averages[i]="$(printf "%.0f" $(ping -c 2 $mirror|tail -n1|cut -d/ -f5))"

DaWei, I'll remember that for other languages.

Quote:
Originally Posted by jim mcnamara
stick a :; between do and done That's a colon semicolon
That worked too By the way, does : ever do anything?

Quote:
Originally Posted by jim mcmamara
Finally, I don't understand what you mean by reference $i. You want to get
the value of i outside the function? Or the value of the array element referenced by i - the array you declare inside the function?

Note: i only has scope during the "for" loop inside the getmirror function. The array "averages" only has scope inside the getmirror function.

They do not exist outside their scope.
1. See the comment on line 5.
2. Reread #1 in the first post. (If I referenced the array element referenced by $i I'd just get the best average, not the fastest domain.)

Meh, I gotta get used to going from 8 VDs to 12.

Oo 100 posts!
__________________
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.
a thing is offline   Reply With Quote
Old Sep 24th, 2006, 5:40 PM   #6
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've got it! $(eval "echo $""$i")
__________________
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; Sep 24th, 2006 at 5:55 PM.
a thing is offline   Reply With Quote
Reply

Bookmarks

« Previous Thread in Forum | Next Thread in Forum »

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump




DaniWeb IT Discussion Community
All times are GMT -5. The time now is 10:47 AM.

Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC