Loop and variable not exactly variable: what's wrong


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers Loop and variable not exactly variable: what's wrong
# 1  
Old 06-30-2012
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
# 2  
Old 06-30-2012
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 08:04 AM..
# 3  
Old 06-30-2012
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
So youve got the logic right now ? Smilie
# 5  
Old 06-30-2012
Yes, the logic, thanks vbe. I've got some reading to do now Smilie

---------- 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:
Mod Comment 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 09:04 AM..
 
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Storing the value of a single query to a Variable - What am i doing wrong?

I tried the below : #!/bin/ksh testvar=$( sqlplus $user/$pass << EOF set pagesize 0 feedback off verify off heading off echo off; whenever sqlerror exit 1 SELECT 1+1 FROM DUAl; COMMIT; EXIT; EOF) echo "testvar="$testvar The output that I am getting is : > sh example.sh... (3 Replies)
Discussion started by: SriRamKrish
3 Replies

2. Shell Programming and Scripting

[Solved] How to increment and add variable length numbers to a variable in a loop?

Hi All, I have a file which has hundred of records with fixed number of fields. In each record there is set of 8 characters which represent the duration of that activity. I want to sum up the duration present in all the records for a report. The problem is the duration changes per record so I... (5 Replies)
Discussion started by: danish0909
5 Replies

3. Shell Programming and Scripting

What's wrong after assign it to a variable ?

root@intel_5500_server:/tmp# ID_GUEST="01:00.0" root@intel_5500_server:/tmp# echo $ID_GUEST|sed 's%:%\\:%g' 01\:00.0 root@intel_5500_server:/tmp# ID_GUEST_P=`echo $ID_GUEST|sed 's%:%\\:%g'` root@intel_5500_server:/tmp# echo $ID_GUEST_P 01:00.0 root@intel_5500_server:/tmp# if I assign... (1 Reply)
Discussion started by: yanglei_fage
1 Replies

4. Shell Programming and Scripting

Array Variable being Assigned Values in Loop, But Gone when Loop Completes???

Hello All, Maybe I'm Missing something here but I have NOOO idea what the heck is going on with this....? I have a Variable that contains a PATTERN of what I'm considering "Illegal Characters". So what I'm doing is looping through a string containing some of these "Illegal Characters". Now... (5 Replies)
Discussion started by: mrm5102
5 Replies

5. Shell Programming and Scripting

printing variable with variable suffix through loop

I have a group of variables myLINEcnt1 - myLINEcnt10. I'm trying to printout the values using a for loop. I am at the head banging stage since i'm sure it has to be a basic syntax issue that i can't figure out. For myIPgrp in 1 2 3 4 5 6 7 8 9 10; do here i want to output the value of... (4 Replies)
Discussion started by: oly_r
4 Replies

6. Shell Programming and Scripting

[SHELL: /bin/sh] For loop using variable variable names

Simple enough problem I think, I just can't seem to get it right. The below doesn't work as intended, it's just a function defined in a much larger script: CheckValues() { for field in \ Group_ID \ Group_Title \ Rule_ID \ Rule_Severity \ ... (2 Replies)
Discussion started by: Vryali
2 Replies

7. Shell Programming and Scripting

Help with variable using loop

Hi I have webserver that I do read data from. Data are stored like this: Huston |1 Portland |2 Hazen |1 Minneapolis |4 Albany |1 Pittsburg |1 Albany |1 Huston |1 Portland|1 Hazen |2 Albany |2 Huston |1 Hazen |1 Script #!/bin/sh user="admin" (1 Reply)
Discussion started by: Jotne
1 Replies

8. Shell Programming and Scripting

How to define a variable with variable definition is stored in a variable?

Hi all, I have a variable say var1 (output from somewhere, which I can't change)which store something like this: echo $var1 name=fred age=25 address="123 abc" password=pass1234 how can I make the variable $name, $age, $address and $password contain the info? I mean do this in a... (1 Reply)
Discussion started by: freddy1228
1 Replies

9. Shell Programming and Scripting

variable inside variable inside loop headache

Hi Gurus I have a file called /tmp/CMDB which looks like this serial: 0623AN1208 hostname: server1 model: x4100 assetID: 1234 I am writing a for loop that will go through this file line by line creating a variable of itself. Using the first iteration of the loop (i.e. the first line) as... (6 Replies)
Discussion started by: hcclnoodles
6 Replies

10. Shell Programming and Scripting

What wrong with the variable definition

i am using the below script and trying to move files in that directory in that pattern to archive. But it doesn;t seem to take the metacharacters. Please sugggest. Code Debug output: (1 Reply)
Discussion started by: dsravan
1 Replies
Login or Register to Ask a Question