Home Man
Search
Today's Posts
Register

If you're not sure where to post a Unix or Linux question, post it here. All unix and Linux beginners welcome in this forum!

Bash array variables are changed in loop runtime

Tags
array variables, shell script

👤 Login to reply

 
Thread Tools Search this Thread
# 1  
Old 11-15-2016
Bash array variables are changed in loop runtime

I am trying to check whether particular host and port are responding or not. I am using below script to check. but node_port array that i am using in loop is getting replaced with previous iteration value. Script and output is given.

Please help me to understanding why node_port values are replaced and how to avoid that.

I am using bash shell for executing this.

Code:
host_names=(xx.yy.zz.167 xx.yy.zz.168 xx.yy.zz.169 xx.yy.zz.170 xx.yy.zz.171 xx.yy.zz.172)
node_port=(2100 2200 2300 2400 2500)

while :
do
        LoopstartTime=$(date '+%Y-%m-%d %r %Z')
        unset i j
        for i in "${host_names[@]}"
        do
                echo "First Loop-$i"
                for j in "${node_port[@]}"
                do
                  echo "        Second Loop-$j"
                  echo "        variables list ${node_port[@]}"
                  echo "        no of arg ${#node_port[@]}"
                  unset j
                done
        done
        LoopendTime=$(date '+%Y-%m-%d %r %Z')
        echo "Process started at ${LoopstartTime} and Completed at ${LoopendTime}"
        echo " "
        sleep $wait_time
done

Output is
Quote:
First Loop-xx.yy.zz.167
Second Loop-2100
variables list 2100 2200 2300 2400 2500
no of arg 5
Second Loop-2200
variables list 2100 2200 2300 2400 2500
no of arg 5
Second Loop-2300
variables list 2200 2200 2300 2400 2500
no of arg 5
Second Loop-2400
variables list 2300 2200 2300 2400 2500
no of arg 5
Second Loop-2500
variables list 2400 2200 2300 2400 2500
no of arg 5

Last edited by tmalik79; 11-16-2016 at 12:28 AM.. Reason: formating
# 2  
Old 11-15-2016
The output doesn't seem to belong to the script?
And, the node_port array is NOT altered by that script.
# 3  
Old 11-16-2016
Hi Rudic, Thanks for quick response.

Output is from above script, I just modified IP address due to sensitivity of the data. when i executed above script thats what exactly printed on screen.

Please let me know if you have any questions.
# 4  
Old 11-16-2016
Please DON'T edit posts after people commented on it, pulling the rug from under their feet!

Running that script gives the expected output, and none of the variables is modified. What is the error that you think you encounter?
And, where has the
Quote:
Fail Triggered for xx.yy.zz.167 2500 at 2016-11-15 05:45:06 AM PST
from the original post#1 gone?
# 5  
Old 11-16-2016
Sorry for that.

Quote:
Fail Triggered for xx.yy.zz.167 2500 at 2016-11-15 05:45:06 AM PST
is removed as that is not relevant.
I am not encountering error but my array sequence is getting changed in run time.

if you observer output every time second loop is completed
Code:
node_port

variable are updated or sequence is changed.

Please go through ouput once. that has node_port array output.
# 6  
Old 11-16-2016
I can't reproduce the output you posted when running the script you posted. Nor are the input arrays altered in any way.
The idiosyncrasies mentioned must come from some parts of the script NOT posted.
👤 Login to reply

« Previous Thread | Next 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
Bash for loop array mwheeler12 Shell Programming and Scripting 5 03-27-2018 10:50 AM
Bash - array loop performance math4 Shell Programming and Scripting 15 10-02-2014 06:14 AM
Bash for loop with awk and variables lily-anne Shell Programming and Scripting 2 08-15-2014 11:28 AM
awk loop using array:wish to store array values from loop for use outside loop klane Shell Programming and Scripting 2 12-19-2013 03:25 PM
Bash for loop with two variables alex2005 Shell Programming and Scripting 3 09-11-2013 04:30 PM
ksh Array name determined at runtime bs103950 Shell Programming and Scripting 9 04-17-2013 12:17 PM
For loop; how to construct a array with variables andresgom Shell Programming and Scripting 4 04-10-2013 11:17 PM
problem access array outside of loop in bash adlmostwanted Shell Programming and Scripting 4 12-12-2011 11:18 AM
Using variables created sequentially in a loop while still inside of the loop [bash] DeCoTwc Shell Programming and Scripting 2 06-23-2009 04:59 PM
declare, assign variables using array, counter, loop egkumpe Shell Programming and Scripting 3 08-09-2004 10:56 AM


All times are GMT -4. The time now is 05:14 AM.

Unix & Linux Forums Content Copyrightę1993-2018. All Rights Reserved.
UNIX.COM Login
Username:
Password:  
Show Password