LinuxQuestions.org
Support LQ: Use code LQ3 and save $3 on Domain Registration
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 02-10-2011, 10:32 PM   #1
russell3901
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
do

<login to router and do stuff>

done < devices.txt
--------------------

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

device1
device2
device3
device4
etc.

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?

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

Rep: Reputation: 943Reputation: 943Reputation: 943Reputation: 943Reputation: 943Reputation: 943Reputation: 943Reputation: 943
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
Code:
#!/bin/bash
while read device dummy ; do
    (
        # Login to "$device", do the work
    ) &
done < devices.txt
wait
If you need the conversation log from each device, you can do eg.
Code:
#!/bin/bash
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
wait
while read device dummy ; do
    if [ -e "$WORK/${device//\//_}.ok" ]; then
        echo "$device: Success" >&2
    else
        echo "$device: Failure" >&2
    fi
    cat "$WORK/${device//\//_}.log"
    echo
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 09:27 AM.
 
  


Reply

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


All times are GMT -5. The time now is 10:15 PM.

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
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration