LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 04-06-2014, 05:37 AM   #1
massy
Member
 
Registered: Nov 2013
Distribution: CentOS 6.4
Posts: 209
Blog Entries: 1

Rep: Reputation: Disabled
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
 
Old 04-06-2014, 08:00 AM   #2
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 10,006

Rep: Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191
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
 
Old 04-07-2014, 12:54 PM   #3
rtmistler
Moderator
 
Registered: Mar 2011
Location: USA
Distribution: MINT Debian, Angstrom, SUSE, Ubuntu, Debian
Posts: 9,882
Blog Entries: 13

Rep: Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930
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?
 
1 members found this post helpful.
  


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
how to loop over text file lines within bash script for loop? johnpaulodonnell Linux - Newbie 9 07-28-2015 03:49 PM
Bash script issue (for loop inside a loop) Mperonen Programming 3 08-08-2013 02:14 AM
finished jobs on linux cluster zain.hp Linux - Newbie 1 11-20-2011 03:20 PM
[SOLVED] Bash - While Loop reading from two lists simultaneously - nested while loop wolverene13 Programming 11 10-01-2011 05:00 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 09:49 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