Bash array variables are changed in loop runtime


Login or Register for Dates, Times and to Reply

 
Thread Tools Search this Thread
Top Forums UNIX for Beginners Questions & Answers Bash array variables are changed in loop runtime
# 1  
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 01:28 AM.. Reason: formating
# 2  
The output doesn't seem to belong to the script?
And, the node_port array is NOT altered by that script.
# 3  
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  
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  
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  
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 or Register for Dates, Times and to Reply

Previous Thread | Next Thread
Thread Tools Search this Thread
Search this Thread:
Advanced Search

Test Your Knowledge in Computers #312
Difficulty: Easy
Linus Torvalds created Unix and Git.
True or False?

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Bash for loop array

Hi there, A bit new to bash and am having an issue with a for loop. I look for filenames in a specified directory and pull the date string from each meeting a certain criteria, and then would like to make a directory for each date found, like this: search 20180101.gz 20180102.gz 20180103.gz... (5 Replies)
Discussion started by: mwheeler12
5 Replies

2. Shell Programming and Scripting

Bash - array loop performance

Hi, another little question... "sn" is an array whose elements can vary from about 55,000 to about 150,000 elements. Each element consists of an integer between 0-255, eg: ${sn} contain the value: 103 . For a decrypt-procedure I need scroll all the elements 4 or 5 times. Here is an example of... (15 Replies)
Discussion started by: math4
15 Replies

3. Shell Programming and Scripting

Bash for loop with awk and variables

I'm relatively new to bash scripting and am trying to use awk inside a bash for loop but am having a problem with the variables. for i in $(seq 30 5 60) do # Define variables up and down in AWK eval $(awk 'BEGIN{ print "up=$((i+1))"}' < /dev/null) eval $(awk 'BEGIN{ print... (2 Replies)
Discussion started by: lily-anne
2 Replies

4. Shell Programming and Scripting

awk loop using array:wish to store array values from loop for use outside loop

Here's my code: awk -F '' 'NR==FNR { if (/time/ && $5>10) A=$2" "$3":"$4":"($5-01) else if (/time/ && $5<01) A=$2" "$3":"$4-01":"(59-$5) else if (/time/ && $5<=10) A=$2" "$3":"$4":0"($5-01) else if (/close/) { B=0 n1=n2; ... (2 Replies)
Discussion started by: klane
2 Replies

5. Shell Programming and Scripting

Bash for loop with two variables

Hi, I have the following folder structure: TST500-1000 TST500-2000 TST500-3000 TST700-1000 TST700-2000 TST700-3000 TST900-1000 TST900-2000 TST900-3000 I would like to store the numbers in the first column (considering "-" as column separator) into a variable then the numbers in... (3 Replies)
Discussion started by: alex2005
3 Replies

6. Shell Programming and Scripting

For loop; how to construct a array with variables

Hi everybody!! Here is the thing; I have a trouble in this simple situation, I'm trying to write an array with all the arguments of a command. I mean, if I have: ./mycommand.sh aa bb cc dd I need to take an array like this: myarray=(aa bb cc dd) So I use a simple for loop like this: for... (4 Replies)
Discussion started by: andresgom
4 Replies

7. Shell Programming and Scripting

problem access array outside of loop in bash

Below is a test script I was trying to use so that I could understand why the logic was not working in a larger script. While accessing and printing array data inside the while loop, everything is fine. Outside the loop, i guess everything is null?? The for loop that is meant to cycle... (4 Replies)
Discussion started by: adlmostwanted
4 Replies

8. Shell Programming and Scripting

variables can not be changed

I was trying to get the free space using df, however, it can not be obtained. Is there anyone could help me. Here is my code: #!/bin/bash #The free space of the tmp file; free_space="0" #Experimental path where the data file is stored into it. #This path should be provided by the first... (3 Replies)
Discussion started by: chenfu
3 Replies

9. Shell Programming and Scripting

Using variables created sequentially in a loop while still inside of the loop [bash]

I'm trying to understand if it's possible to create a set of variables that are numbered based on another variable (using eval) in a loop, and then call on it before the loop ends. As an example I've written a script called question (The fist command is to show what is the contents of the... (2 Replies)
Discussion started by: DeCoTwc
2 Replies

10. Shell Programming and Scripting

declare, assign variables using array, counter, loop

using bash Tru64... converting DCL to shell... any tips to make this work would be greatly appreciated. Below is my failed attempt to assign command line input to variables by first declaring an array. I use a counter to create unique variables in a loop through the array. I need to call... (3 Replies)
Discussion started by: egkumpe
3 Replies

Featured Tech Videos