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.
I've been trying to find the issue for the last couple of days now and I'm apparently not processing this very well. It seems as though everytime I get an error, I fix that one and another one pops up. The only thing I can think of is that there's something I'm overlooking that would set it all straight. As stupid as the script looks, it's slightly functional for me but it's also an educational experience. Please don't suggest python or perl as a fix. There has to be a way to do it within bash.
At the time of this writing, here is the script:
$downloadop is the output file from wget
Looks problematic to me. The "$( blah )" syntax means to replace with the output of the command in parens. I'm not sure exactly what you're trying to do, but I think maybe you want this:
Code:
avgrate=$(($a/$count))
The "$(( blah ))" syntax expands to the result of the mathematical expression contained in double parens.
You basic problem is that you don't need/want the $ in front of the left-hand variable in the = expression. Having it there causes bash to evaluate the variable and then try to evaluate the whole line. Since "408" is not a valid command, that fails.
Looks problematic to me. The "$( blah )" syntax means to replace with the output of the command in parens. I'm not sure exactly what you're trying to do, but I think maybe you want this:
Code:
avgrate=$(($a/$count))
The "$(( blah ))" syntax expands to the result of the mathematical expression contained in double parens.
Basically I'm trying to convert the speed output from each line of wget to kbps or mbps rather than KB/s or MB/s. The purpose of all the math is because wget will output 1006KB/s but it will also output 1.11MB/s as opposed to 1110KB/s. So there has to be conversion somewhere in there.
While your suggestion will undoubtedly save me some errors later, it didn't change the errors I'm currently getting where the line:
Code:
$nextline=$(( $nextline*1000 ))
is being interpreted as a command for some reason.
On a line like this, the leading $ on $nextline= is causing problems - remove that. Also, while I have not found it to seem to make a difference, the $ on variables inside $(( )) evaluations is not needed. So:
You basic problem is that you don't need/want the $ in front of the left-hand variable in the = expression. Having it there causes bash to evaluate the variable and then try to evaluate the whole line. Since "408" is not a valid command, that fails.
If I understood your suggestion correctly, I made the appropriate changes and now I'm getting the following errors:
Code:
+ cat /home/buee/Desktop/OutputFile.txt
+ awk '{print $8}'
+ grep M
+ tr -d '[A-Z][a-z]'
+ zero=0
++ wc -l tmp.txt
++ cut -f1 '-d '
+ count=864
+ (( count++ ))
./Speedtest.sh: line 21: syntax error near unexpected token `('
./Speedtest.sh: line 21: `kilos=(( $kilos * 1000 ))'
+ echo
./Speedtest.sh: line 23: syntax error near unexpected token `done'
./Speedtest.sh: line 23: `done'
Here's the code just to make sure I didn't understand correctly:
Also, while I have not found it to seem to make a difference, the $ on variables inside $(( )) evaluations is not needed.
Quite true, but Bash allows the "$" prefix to variable names within arithmetic expansion expressions. I prefer to use it for the sake of consistency since the "$" prefix to variable names is required in other instances such as "[ blah ]" conditional expressions.
Quote:
Originally Posted by `The GNU Bash Reference Manual' Version 3.2, section 6.5
Within an expression,
shell variables may also be referenced by name without using the
parameter expansion syntax.
After some tweaking, I've gotten through the script without errors. However, there appears to be some mathematical issues somewhere within the script. The end result is telling me that I'm doing 776kbps or 97KB/s. This cannot be correct. Looking through the tmp files, I'm seeing an average (just ballparking, nothing set in stone) of about 900KB/s. Now, either my script is doing something screwy with the math (rounding produces a huge margin of error perhaps?) or wget doesn't know how to distinguish KB/s (kilobytes per second) from kbps (kilobits per second). When running an HTTP speedtest from the same connection to the same server, I usually range around 9mbps which should be a little over 1MB/s, so there's definitely a rather large error somewhere. Sounds alot like the virus Michael Bolton programmed in Office Space, doesn't it?
Run your script with sh -x yourscript, and much will become clear about where errors are made.
When I have to do calculations in Bash scripts which go beyond a simple addition, I use dc.
Example:
Code:
result=$("echo 12 14 * 3 / p" | dc)
(This is RPN notation)
Finally, I would propose you do not use a Linux site for speed tests. I have done so as well until I realized that I was just eating up bandwidth mostly from non-profit organizations. I use this link for speed testing: http://download.microsoft.com/downlo...a/dotnetfx.exe
Run your script with sh -x yourscript, and much will become clear about where errors are made.
When I have to do calculations in Bash scripts which go beyond a simple addition, I use dc.
Example:
Code:
result=$("echo 12 14 * 3 / p" | dc)
(This is RPN notation)
Finally, I would propose you do not use a Linux site for speed tests. I have done so as well until I realized that I was just eating up bandwidth mostly from non-profit organizations. I use this link for speed testing: http://download.microsoft.com/downlo...a/dotnetfx.exe
jlinkels
Ok, so I've tweaked it here and there and got the processing time down a pretty good amount.
This is working pretty well and it's not sucking bandwidth from a good company. I decided that, rather than processing 13,000K lines and doing the math on those, I could grab a random number of lines and calculate those (using rl -c 1500 which is randomize-lines in the Ubu repos. In the interest of tracking this and on the off chance someone would want to use the script, here are the results from it:
The first few lines are self explanatory
The line with seemingly random numbers is the sum of all speeds added together and the number of lines in the file
The first time (military) is when the whole script starts
The second time " " is when the download finishes and the calculations begin
The third time " " is when the whole process completes
Code:
This is with Ubuntu Distro, all lines calculated
buee@buee-desktop:~/Desktop$ ./Speedtest.sh
697 KB/s
5576 kbps
9685541 13877
22:40
22:59
23:27
This is the XP SP2, all lines calculated
965 KB/s
7720 kbps
5262099 5449
15:14
15:19
15:35
This is the XP SP2, 1000 lines calculated
1077 KB/s
8616 kbps
1080124 1002
15:54
15:58
16:01
This is the XP SP2, all lines calculated (Same Download is interpreted as above)
1045 KB/s
8360 kbps
5694547 5449
16:06
16:06
16:23
This is the XP SP2, 2000 calculated (Same Download is interpreted as above)
1027 KB/s
8216 kbps
2056891 2002
16:25
16:25
16:33
This is the XP SP2, 3000 calculated (Same Download is interpreted as above)
1024 KB/s
8192 kbps
3074153 3002
17:06
17:06
17:20
This is the XP SP2, 3000 calculated (Same Download is interpreted as above)
1046 KB/s
8368 kbps
3142354 3002
17:31
17:31
17:40
This is the XP SP2, 3000 calculated (Same Download is interpreted as above)
1017 KB/s
8136 kbps
3055990 3002
18:09
18:09
18:18
The bottom 3 entries are all from the same download files so the first two times won't change and I wanted to test randomness and accuracy. As you can see, I trimmed down the whole script run time significantly. Just adjust the number of random lines per your machine's capability and accuracy requirements and this should be a pretty good gauge of legitimate internet speeds.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.