LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This 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


Reply
  Search this Thread
Old 11-30-2011, 12:07 PM   #1
ali2011
Member
 
Registered: Nov 2011
Location: USA, CA
Distribution: Ubuntu+Fedora
Posts: 80

Rep: Reputation: Disabled
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
 
Old 11-30-2011, 02:35 PM   #2
John VV
LQ Muse
 
Registered: Aug 2005
Location: A2 area Mi.
Posts: 17,624

Rep: Reputation: 2651Reputation: 2651Reputation: 2651Reputation: 2651Reputation: 2651Reputation: 2651Reputation: 2651Reputation: 2651Reputation: 2651Reputation: 2651Reputation: 2651
Do not post your homework !
that is for YOU to do .

That error looks a bit small ,as in not the FULL error .
 
1 members found this post helpful.
Old 12-01-2011, 05:34 PM   #3
salasi
Senior Member
 
Registered: Jul 2007
Location: Directly above centre of the earth, UK
Distribution: SuSE, plus some hopping
Posts: 4,070

Rep: Reputation: 897Reputation: 897Reputation: 897Reputation: 897Reputation: 897Reputation: 897Reputation: 897
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 View Post
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 View Post
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.
 
Old 12-02-2011, 03:01 AM   #4
8-bit
Member
 
Registered: Jan 2009
Location: Southern Oregon under a rock.
Distribution: Puppy 431 SCSI, Lucid 520, Slacko, Win 7
Posts: 131

Rep: Reputation: 49
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.
 
Old 12-02-2011, 10:05 AM   #5
ali2011
Member
 
Registered: Nov 2011
Location: USA, CA
Distribution: Ubuntu+Fedora
Posts: 80

Original Poster
Rep: Reputation: Disabled
Quote:
Originally Posted by John VV View Post
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.
 
0 members found this post helpful.
Old 12-02-2011, 10:28 AM   #6
Hevithan
Member
 
Registered: Apr 2011
Location: Washington State
Distribution: Zorin5-(Ubuntu 11.04) // Backtrack 5-(Ubuntu 10.04) // Dreamlinux 3.5-(Debian)
Posts: 275
Blog Entries: 5

Rep: Reputation: 40
Quote:
Originally Posted by ali2011 View Post
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?


Try this link: http://lmgtfy.com/?q=bash%3A+syntax+...+token+%60(%27 ... I found the answer there, so I know it can't be that hard. Good luck.

Last edited by Hevithan; 12-02-2011 at 10:41 AM.
 
Old 12-03-2011, 03:49 AM   #7
salasi
Senior Member
 
Registered: Jul 2007
Location: Directly above centre of the earth, UK
Distribution: SuSE, plus some hopping
Posts: 4,070

Rep: Reputation: 897Reputation: 897Reputation: 897Reputation: 897Reputation: 897Reputation: 897Reputation: 897
Quote:
Originally Posted by ali2011 View Post
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 View Post
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).
 
Old 12-04-2011, 06:03 AM   #8
Disillusionist
Senior Member
 
Registered: Aug 2004
Location: England
Distribution: Ubuntu
Posts: 1,039

Rep: Reputation: 98
Quote:
Originally Posted by ali2011 View Post
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.
 
1 members found this post helpful.
Old 12-05-2011, 01:45 AM   #9
colucix
LQ Guru
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

Rep: Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983
Quote:
Originally Posted by ali2011 View Post
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.
 
1 members found this post helpful.
Old 12-05-2011, 03:40 PM   #10
Disillusionist
Senior Member
 
Registered: Aug 2004
Location: England
Distribution: Ubuntu
Posts: 1,039

Rep: Reputation: 98
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)

If you replace:
Code:
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'
with:
Code:
if [[ ! -s $loop.sed ]]
then
   iperf -c $loop -u -b 4M > $loop.iperf-udp-4
fi
You remove a lot of unneccessary complication, and fix your use of if which I still believe is the main issue you have been experiencing.
 
  


Reply



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 Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
need bandwith traffic measure by ip aduntoredas Linux - Networking 1 05-26-2011 01:57 AM
Measure script mahmoud Programming 4 06-12-2009 02:41 PM
Bandwith on a network card rincewind Linux - Software 2 06-04-2006 06:33 PM
Rsync Bandwith saver script pixie Linux - General 0 06-03-2004 03:19 AM
simple bandwith measure fariz83 Linux - Networking 2 03-28-2004 03:24 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 06:08 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration