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
By cramming it all onto one line you've made it almost illegible.
your awk statement does nothing. variables inside awk don't get set outside awk. awk is its own programming language, not part of the shell.
This is a dangerous use of backticks and useless use of cat.
Instead of using several temporary files, you could have just used pipes. You might even have been able to just check iperf's return value and saved no files at all.
The shell has its own way of checking if a file is empty, no need to use du and awk.
Shell doesn't use () for if-statements. Shell is not C.
if ($c == 1); ./iperf -c $loop -u -b 4M > $loop.iperf-udp-4[/code]
You've got two done's and only one 'do'.
I'll post corrections in a minute.
---------- Post updated at 12:59 PM ---------- Previous update was at 12:54 PM ----------
Can you kindly please check what's wrong here (the full script).
while read machine do echo " "; echo "# Outer loop: $machine"; echo "==================="; ssh -i /~/~/~/~/~ ~@$machine 'cd iperf; while read loop do echo "+ Inner loop: $loop"; until ./iperf -c $loop -u -b 4M > $loop.iperf-udp-4 && grep -q "%" $loop.iperf-udp-4 do echo "$loop failed, retrying"; done; done < list.10'; done < w1
Error:
bash: syntax error near unexpected token `done'
---------- Post updated at 03:11 PM ---------- Previous update was at 02:29 PM ----------
Thanks alot; It's okay now. BTW, how can I set the inside until loop to run only for 5 times for each file, if no success after the 5's, break to the next loop?
Cramming it all on line line like that has, again, made it completely illegible. Unless you're trying to save your enter key for later, you're not doing yourself a favor.