Download your favorite Linux distribution at LQ ISO.
Go Back > Forums > Non-*NIX Forums > Programming
User Name
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.


  Search this Thread
Old 02-10-2011, 09:32 PM   #1
LQ Newbie
Registered: Feb 2011
Posts: 1

Rep: Reputation: 0
do while loop, but all at once?

Hi All,

Currently I run do-while loops to login to a number of devices.

like so;

while read line

<login to router and do stuff>

done < devices.txt

I have my device list in devices.txt in the following format;


While this works very well, its time consuming with alot of devices.
Is there a way to run though the device list concurrently (via do while or another method) so it hits them all at the same time?

Old 02-10-2011, 10:53 PM   #2
Nominal Animal
Senior Member
Registered: Dec 2010
Location: Finland
Distribution: Xubuntu, CentOS, LFS
Posts: 1,723
Blog Entries: 3

Rep: Reputation: 946Reputation: 946Reputation: 946Reputation: 946Reputation: 946Reputation: 946Reputation: 946Reputation: 946
Yes. If you don't need the output (or don't mind any output being a mashup of all), and don't care about their exit statuses, you can just do
while read device dummy ; do
        # Login to "$device", do the work
    ) &
done < devices.txt
If you need the conversation log from each device, you can do eg.
WORK="`mktemp -d`" || exit $?
trap "rm -rf '$WORK'" EXIT
while read device dummy ; do
    ( exec &>"$WORK/${device//\//_}.log"

      # Do the login stuff.
      # Use 'exit' to quit early, if problems occur.

      # Only do this if success.
      touch "$WORK/${device//\//_}.ok"
    ) &
done < devices.txt
while read device dummy ; do
    if [ -e "$WORK/${device//\//_}.ok" ]; then
        echo "$device: Success" >&2
        echo "$device: Failure" >&2
    cat "$WORK/${device//\//_}.log"
done < devices.txt
Note that you can add further files into $WORK (for example, describing errors if any), and not worry about them being cleaned up, because the trap will always remove it and its contents automatically when the script exits, no matter the reason.
The script will also exit with failure (nonzero status) if any of the device logins failed.

Does this work for you, or do you also need interactive input for the login processes?
Nominal Animal

Last edited by Nominal Animal; 03-21-2011 at 08:27 AM.


bash, sh, shell script

Thread Tools Search this Thread
Search this Thread:

Advanced Search

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
for loop or while loop to read the fields of a file.. visitnag Linux - Newbie 10 09-02-2010 08:47 PM
bash loop within a loop for mysql ops br8kwall Programming 10 04-30-2008 03:50 AM
converting shell while loop to for loop farkus888 Programming 8 09-12-2007 02:30 AM

All times are GMT -5. The time now is 07:12 PM.

Main Menu
Write for LQ is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration