Unix/Linux Go Back    


UNIX for Beginners Questions & Answers 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

UNIX for Beginners Questions & Answers


Tags
array variables, shell script

Closed    
 
Thread Tools Search this Thread Display Modes
    #1  
Old Unix and Linux 11-15-2016   -   Original Discussion by tmalik79
tmalik79's Unix or Linux Image
tmalik79 tmalik79 is offline
Registered User
 
Join Date: Sep 2011
Last Activity: 16 November 2016, 6:18 AM EST
Posts: 27
Thanks: 3
Thanked 0 Times in 0 Posts
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
Sponsored Links
    #2  
Old Unix and Linux 11-15-2016   -   Original Discussion by tmalik79
RudiC's Unix or Linux Image
RudiC RudiC is online now Forum Staff  
Moderator
 
Join Date: Jul 2012
Last Activity: 23 April 2018, 5:23 AM EDT
Location: Aachen, Germany
Posts: 12,507
Thanks: 401
Thanked 3,878 Times in 3,565 Posts
The output doesn't seem to belong to the script?
And, the node_port array is NOT altered by that script.
Sponsored Links
    #3  
Old Unix and Linux 11-16-2016   -   Original Discussion by tmalik79
tmalik79's Unix or Linux Image
tmalik79 tmalik79 is offline
Registered User
 
Join Date: Sep 2011
Last Activity: 16 November 2016, 6:18 AM EST
Posts: 27
Thanks: 3
Thanked 0 Times in 0 Posts
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 Unix and Linux 11-16-2016   -   Original Discussion by tmalik79
RudiC's Unix or Linux Image
RudiC RudiC is online now Forum Staff  
Moderator
 
Join Date: Jul 2012
Last Activity: 23 April 2018, 5:23 AM EDT
Location: Aachen, Germany
Posts: 12,507
Thanks: 401
Thanked 3,878 Times in 3,565 Posts
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?
Sponsored Links
    #5  
Old Unix and Linux 11-16-2016   -   Original Discussion by tmalik79
tmalik79's Unix or Linux Image
tmalik79 tmalik79 is offline
Registered User
 
Join Date: Sep 2011
Last Activity: 16 November 2016, 6:18 AM EST
Posts: 27
Thanks: 3
Thanked 0 Times in 0 Posts
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.
Sponsored Links
    #6  
Old Unix and Linux 11-16-2016   -   Original Discussion by tmalik79
RudiC's Unix or Linux Image
RudiC RudiC is online now Forum Staff  
Moderator
 
Join Date: Jul 2012
Last Activity: 23 April 2018, 5:23 AM EDT
Location: Aachen, Germany
Posts: 12,507
Thanks: 401
Thanked 3,878 Times in 3,565 Posts
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.
Sponsored Links
Closed

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Linux More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
Bash - array loop performance math4 Shell Programming and Scripting 15 10-02-2014 06:14 AM
Bash for loop with two variables alex2005 Shell Programming and Scripting 3 09-11-2013 04:30 PM
For loop; how to construct a array with variables andresgom Shell Programming and Scripting 4 04-10-2013 11:17 PM
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:24 AM.