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
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.
---------- Post updated at 12:59 PM ---------- Previous update was at 12:54 PM ----------
Code:
# much faster and more efficient and less dangerous than `cat file.10`
while read loop
do
echo "+ Inner loop: $loop";
# The program's likely telling you whether it succeeds or fails via
# its return code, a special variable that isn't printed to the screen
# but just set when a program finishes. An if or while statement
# can use it directly.
#
# The ! inverts its meaning, so this repeats whenever iperf fails.
while ! ./iperf -c $loop -u -b 4M > $loop.iperf-udp-4
do
echo "$loop failed, retrying"
done
done < list.10
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.
An outline:
Code:
TRIES=0
until ./iperf ...
do
if [ "$TRIES" -ge 5 ]
then
echo "Too many tries"
break
fi
echo "retrying"
TRIES=`expr $TRIES + 1`
done