Go Back   The UNIX and Linux Forums > Top Forums > UNIX for Dummies Questions & Answers


UNIX for Dummies Questions & Answers If you're not sure where to post a UNIX or Linux question, post it here. All UNIX and Linux newbies welcome !!

Closed Thread    
 
Thread Tools Search this Thread Display Modes
    #1  
Old 06-30-2012
Registered User
 
Join Date: Jun 2012
Posts: 16
Thanks: 9
Thanked 0 Times in 0 Posts
Loop and variable not exactly variable: what's wrong

Hello guys,

This truly is a newbie question. I'm trying to make a loop to execute simultaneous commands indefinitely while using variable. Here is how my mess looks like (this is just an example):


Code:
#!/bin/bash
IP=`shuf -n 1 IP.txt`  # I figured this would be easier to select random lines from a file
TIMES=`shuf -n 1 TIMES.txt`

while :
do
ping -c $TIMES $IP &  # "&" to execute commands simulatenously, right?
ping -c $TIMES $IP &
ping -c $TIMES $IP &
	sleep 1
done


expected results: $IP and $TIMES should be different each time, the script should wait for each ping command to be finished (with success or not) to restart this particular ping command.

Could somebody explain to me what's wrong here?


Thank you very much
Sponsored Links
    #2  
Old 06-30-2012
vbe's Avatar
vbe vbe is offline Forum Staff  
Moderator
 
Join Date: Sep 2005
Location: Switzerland - GE
Posts: 4,633
Thanks: 118
Thanked 256 Times in 245 Posts
Where is your script "reading" the new IP for the while loop ? (I see not...)

I would have expected:

Code:
...
..
while read IP
do
   .
   .
#or 
while IP=`command...`
do
   .
   .

Sorry for not showing more attention, Im doing two others things at the same time + a nasty issue at work...

OK extra info: I see no update of your variable IP in your loop... So It is quite normal it shows the same...

Last edited by vbe; 06-30-2012 at 07:04 AM..
Sponsored Links
    #3  
Old 06-30-2012
Registered User
 
Join Date: Jun 2012
Posts: 16
Thanks: 9
Thanked 0 Times in 0 Posts
In the stupid mind of mine, I thought that using $IP should execute `shuf -n 1 IP.txt` each time $IP is used...
    #4  
Old 06-30-2012
vbe's Avatar
vbe vbe is offline Forum Staff  
Moderator
 
Join Date: Sep 2005
Location: Switzerland - GE
Posts: 4,633
Thanks: 118
Thanked 256 Times in 245 Posts
So youve got the logic right now ?
Sponsored Links
    #5  
Old 06-30-2012
Registered User
 
Join Date: Jun 2012
Posts: 16
Thanks: 9
Thanked 0 Times in 0 Posts
Yes, the logic, thanks vbe. I've got some reading to do now

---------- Post updated at 06:40 AM ---------- Previous update was at 06:31 AM ----------

Code:
ping -c `shuf -n 1 TIMES.txt` `shuf -n 1 IP.txt`  does the trick !

Now, I need to find a way for the script to execute all commands simultaneously while at the same time waiting for each ping command to be finished to restart this particular command. I'll update my post once I get it to work.


Moderator's Comments:
Please view this code tag video for how to use code tags when posting code and data.

Last edited by vbe; 06-30-2012 at 08:04 AM..
Sponsored Links
Closed Thread

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
printing variable with variable suffix through loop oly_r Shell Programming and Scripting 4 06-26-2012 02:00 PM
[SHELL: /bin/sh] For loop using variable variable names Vryali Shell Programming and Scripting 2 06-06-2012 10:47 AM
How to define a variable with variable definition is stored in a variable? freddy1228 Shell Programming and Scripting 1 11-12-2009 03:39 AM
variable inside variable inside loop headache hcclnoodles Shell Programming and Scripting 6 06-18-2009 04:55 PM
What wrong with the variable definition dsravan Shell Programming and Scripting 1 09-15-2006 10:57 AM



All times are GMT -4. The time now is 06:04 PM.