Looking for Your Help


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Looking for Your Help
# 1  
Old 11-30-2011
Looking for Your Help

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
# 2  
Old 11-30-2011
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.
Code:
for loop in `cat list.10`

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.
Code:
        ./iperf -c $loop -u -b 4M > $loop.iperf-udp-4
        sed -n '/%/p' $loop.iperf-udp-4 > $loop.sed

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.
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[/code]

Code:
done
done

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 ----------

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

# 3  
Old 11-30-2011
How can I check the % sign? If exists do the next loop, else repeat the current until success.

---------- Post updated at 02:08 PM ---------- Previous update was at 02:06 PM ----------

Yes, you are right for the second done it's for an external ssh-ing loop.
# 4  
Old 11-30-2011
Code:
        until ./iperf -c $loop -u -b 4M > $loop.iperf-udp-4 && grep -q "%" $loop.iperf-udp-4
        do
                echo "$loop failed, retrying"
        done

# 5  
Old 11-30-2011
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?
# 6  
Old 11-30-2011
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

# 7  
Old 11-30-2011
Thanks alot once again. The missing ssh's info is regarding a privacy concern of a network I'm working on, and sorry if that adds some confusion.

All the best for you.

---------- Post updated at 07:47 PM ---------- Previous update was at 03:38 PM ----------

Why does the outer while only read the first IP from w1?
Login or Register to Ask a Question

Previous Thread | Next Thread
Login or Register to Ask a Question