LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   While loop is finished before doing all of jobs! (https://www.linuxquestions.org/questions/programming-9/while-loop-is-finished-before-doing-all-of-jobs-4175500742/)

massy 04-06-2014 05:37 AM

While loop is finished before doing all of jobs!
 
This is the part of my code to ssh some hosts, but the loop would be finished before reading all of IPs in the file named IPvar and then the script is finished!some times it works for 3 IP,some times for 5 ,...
Code:

while read IP
 do
        rm -f rbtdate.txt
        rm -f rbt.txt
        set -vx
        ssh -n  $usr@$IP 'hostname;date +%T;head -1 /dev/ttyS1;uptime;last reboot|head -15' >rbt.txt
        set +vx
        host=`head -1 rbt.txt|cut -c 5-8`
        RSUname=`_RSUhostname`
        findUP=`grep -c $RSUname rsuDownreport`
        if [ "$findUP" != 0 ];then
                lstUPrsu="$lstUPrsu $RSUname isn't Down - "
        fi
.
.
.
done <IPvar


grail 04-06-2014 08:00 AM

Well I see you use set -xv but have it only working for one line of the code. Try switching it on before the loop and seeing what is actually happening

rtmistler 04-07-2014 12:54 PM

To simplify that further, use what grail suggests, but also just put an echo line in your loop. Example:
Code:

set -xv
while read IP
do
    echo "IP value is $IP";
done <IPvar

And then verify that all values in IPvar are read properly.

What's the nature of IPvar, is that just a list? If so and you intend to perform a loop on all entries of IPvar, then why not just a for loop?


All times are GMT -5. The time now is 02:16 AM.