I need help with a shell script to measure network bandwith
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 need help with a shell script to measure network bandwith
I'm trying to write a shell script to do the following job:
Do a for loop in list.10 by iperf-ing (A Linux command, and you can try any command like traceroute if you don't have this installed) each host and then check the result of that iperf-ing. If it's an empty file (report is not correct), do iperf-ing again. The file will be incorrect if doesn't have % sign.
Note: I check whether the file is empty or not by:
1- sed -n '/%/p' $loop.iperf-udp-4 > $loop.sed (print the specific line I'm looking for)
2- du -h $loop.sed > $loop.size (check the size of result from sed)
3- awk '{if ($1 == 0); c=1}' $loop.size; (if size is 0 do iperf-ing again)
Here is the script I'm trying to use:
for loop in `cat list.10`; do echo "+ Inner loop: $loop"; ./iperf -c $loop -u -b 4M > $loop.iperf-udp-4; sed -n '/%/p' $loop.iperf-udp-4 > $loop.sed; du -h $loop.sed > $loop.size; awk '{if ($1 == 0); c=1}' $loop.size; if ($c == 1); ./iperf -c $loop -u -b 4M > $loop.iperf-udp-4; done'; done
Error I got:
bash: syntax error near unexpected token `('
I will appreciate any help on that or any better suggestion
I'm sorry, but I am not going to help directly with this. However, the indirect help that I think you need, in the first instance, is to point out that if you make your thread easy to read, people are more likely to reply in a way that helps more directly.
Quote:
Originally Posted by ali2011
Do a for loop in list.10 by iperf-ing (A Linux command, and you can try any command like traceroute if you don't have this installed) each host and then check the result of that iperf-ing. If it's an empty file (report is not correct), do iperf-ing again. The file will be incorrect if doesn't have % sign.
We haven't seen what other questions you have been asked; is list.10 some file from a previous question, or is it the file you are being asked to use for this example? We don't know. It doesn't seem to be the file that you mention in the next section.
Quote:
Originally Posted by ali2011
Note: I check whether the file is empty or not by:
1- sed -n '/%/p' $loop.iperf-udp-4 > $loop.sed (print the specific line I'm looking for)
2- du -h $loop.sed > $loop.size (check the size of result from sed)
3- awk '{if ($1 == 0); c=1}' $loop.size; (if size is 0 do iperf-ing again)
code is easier to read if you use the 'code' tag (#) - it uses a fixed width font, it is different from the normal text font, and it keeps whitespace intact, and all of those are important in humans reading code, eg
Code:
Note: I check whether the file is empty or not by:
1- sed -n '/%/p' $loop.iperf-udp-4 > $loop.sed (print the specific line I'm looking for)
2- du -h $loop.sed > $loop.size (check the size of result from sed)
3- awk '{if ($1 == 0); c=1}' $loop.size; (if size is 0 do iperf-ing again)
and the next is just unreadable in its original form
Code:
Here is the script I'm trying to use:
for loop in `cat list.10`; do echo "+ Inner loop: $loop"; ./iperf -c $loop -u -b 4M > $loop.iperf-udp-4; sed -n '/%/p' $loop.iperf-udp-4 > $loop.sed; du -h $loop.sed > $loop.size; awk '{if ($1 == 0); c=1}' $loop.size; if ($c == 1); ./iperf -c $loop -u -b 4M > $loop.iperf-udp-4; done'; done
Code:
Error I got:
bash: syntax error near unexpected token `('
You might have an idea what was going on, if the code had been readable...all of those semicolons and that one extended line is a pain in the eyes. (Actually, even with the quote tags, you get the long line and the 'elevator' and even that is more readable than the 'straight text' version; the code tags version is better, and splitting into individual commands is better, for readability.)
Anyway, the error message tells you that there was a problem near a '('. Usually, this happens when something is wrong with the syntax of previous instruction, and the interpreter is 'surprised' to see a '(' come up, when it was expecting the end of the previous instruction.
I am not a great bash coder at all and stumbling through that code, the only thing I saw was two "done" code segments without two matching "do" segments.
It is sometimes the simple stuff that gets overlooked.
Do not post your homework !
that is for YOU to do .
That error looks a bit small ,as in not the FULL error .
What HW are you talking about? You should not through your words left and right because you don't know people you are talking to. Either give your help or be quiet.
What HW are you talking about? You should not through your words left and right because you don't know people you are talking to. Either give your help or be quiet.
I think what was being implied here, is that the way you chose to word your question sure does sound like an assignment. Specifically:
Quote:
Do a for loop in list.10 by iperf-ing (A Linux command, and you can try any command like traceroute if you don't have this installed) each host and then check the result of that iperf-ing. If it's an empty file (report is not correct), do iperf-ing again. The file will be incorrect if doesn't have % sign.
It looks a lot like a test question or textbook excerpt and if people where to do your homework for you, How would you learn?
You should not through your words left and right because you don't know people you are talking to.
I don't know what this was intended to mean, about the 'words left and right', but it doesn't make much sense.
Quote:
Originally Posted by ali2011
Either give your help or be quiet.
It is interesting that you feel able to give people instructions on what they can and cannot do, in response to your question, but do not feel any necessity to obey the rules for this site. Oddly, this happens quite a lot, and it always ends this way, which, assuming that you wanted help, is not a constructive way for a thread to go.
Obviously, when people stick to the rules, this non-constructive progression really doesn't happen. There are reasons that the rules for the site are the way they are, and it actually helps get an answer, if you obey them. On the other hand, if what you wanted was an argument, rather than an answer, the approach you have taken works fine (until a mod decides to take an interest, that is).
Note: I check whether the file is empty or not by:
1- sed -n '/%/p' $loop.iperf-udp-4 > $loop.sed (print the specific line I'm looking for)
2- du -h $loop.sed > $loop.size (check the size of result from sed)
3- awk '{if ($1 == 0); c=1}' $loop.size; (if size is 0 do iperf-ing again)
Here is the script I'm trying to use:
Code:
for loop in `cat list.10`
do
echo "+ Inner loop: $loop"
./iperf -c $loop -u -b 4M > $loop.iperf-udp-4
sed -n '/%/p' $loop.iperf-udp-4 > $loop.sed
du -h $loop.sed > $loop.size
awk '{if ($1 == 0); c=1}' $loop.size
if ($c == 1)
./iperf -c $loop -u -b 4M > $loop.iperf-udp-4
done'
done
Error I got:
bash: syntax error near unexpected token `('
Splitting your script into multiple lines makes it easier to read, and helps to narrow down where the potential issues are.
Please use code tags around code (as others have suggested) but also it is advisable not to try to write one long string as this makes troubleshooting extreemly difficult.
When written this way, I would suggest you look at the if statement first.
What HW are you talking about? You should not through your words left and right because you don't know people you are talking to. Either give your help or be quiet.
Please, take it easy. Indeed, the way you've posted your question resembles a kind of homework. If not you can easily explain it, without addressing other members in a rude way.
Quote:
Originally Posted by John VV
Do not post your homework !
that is for YOU to do .
The same warning to avoid rudeness applies here. Please John, grant the benefit of the doubt and kindly ask for further clarification if you wish. Thank you.
I would do away with the du -h and awk commands entirely, and I am not sure why you are forcing iperf to only run from the current working directory (this should be in /usr/bin)
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.