Array help


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Array help
# 1  
Old 06-20-2008
Java Array help

Hi all,

I have the scenario where i have different categories of servers of 4 pairs, like pairA=4 serves,pairB=4 servers like that. I want to store them in an array with their hostnames, depends on that hostname iam running this script if that falls in any array the script should do something.
I wrote a script which iterates each element in that array and check an if condition but if i include else statement it's not working.
Please take a look and correct and also if there's a better way to do it.

#!/usr/bin/bash
Array1=( HOST1 HOST2 HOST3 HOST4 )
HOSTNAME=`hostname`
for i in ${Array1[@]}
do
Array1[$i]=$i
if [[ "${Array1[$i]}" = $HOSTNAME ]]
then
echo "This is Type 1 server"
Do something here......just for this server and then should exit
fi
done
Then same for next pair of array....

I tried including else but it didn't exit but test for next value in that array which i dont want .

Please help me.


Thanks in advance.
# 2  
Old 06-20-2008
Hi,

Please somebody reply me.

Let me know if you need more information.


Thanks.
# 3  
Old 06-20-2008
In your loop, $i takes the value of each element of the array $Array1. But you are using it as an array index in Array1[$i]. Put an echo $i in the loop to see what it's doing. If you really need to loop over an array, do
Code:
Array1=( HOST1 HOST2 HOST3 HOST4 )
HOSTNAME=`hostname`
for i in $(seq 0 $((${#Array1[@]} - 1)))
do
  if [[ "${Array1[$i]}" = $HOSTNAME ]]
  then
    echo "This is Type 1 server"
  fi
done

But arrays in bash are ugly when you can just do
Code:
hosts="HOST1 HOST2 HOST3 HOST4"
HOSTNAME=`hostname`
for h in $hosts
do
  if [ "$h" = "$HOSTNAME" ]
  then
   ...
  fi
done

# 4  
Old 06-20-2008
Thanks for your reply. I inserted echo $i and it displayed each elements of array1 in each line.
I would like to know if i declare 2 or 3 arrays with different set of hosts, within a single for loop will i be able to jump from array to array if the hostname not matches. so the script will now have multiple for loops.
Say if
Array1="HOST1 HOST2 HOST3 HOST4"
Array2="HOST5 HOST6 HOST7 HOST8"
Array3="HOST9 HOST10 HOST11 HOST12"
for should include all the elements(Array1+Array2+Array3) but the if loop should maches $HOSTNAME and each elements of Array1 and if not elif should try match for next Array2 and so on and let me know what array the hostname matches.
Also is it ok if i dont give any else condition in an if loop? if the if condition matches will it come out of the loop even if i didnt give any break statement?


Thanks.

Last edited by Jartan; 06-20-2008 at 01:07 PM..
# 5  
Old 06-20-2008
Hi,

Please never mind,i figured it out myself.
Thanks spirtle for your reply.


Best Regards.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Help reading the array and sum of the array elements

Hi All, need help with reading the array and sum of the array elements. given an array of integers of size N . You need to print the sum of the elements in the array, keeping in mind that some of those integers may be quite large. Input Format The first line of the input consists of an... (1 Reply)
Discussion started by: nishantrefound
1 Replies

2. Shell Programming and Scripting

Bash arrays: rebin/interpolate smaller array to large array

hello, i need a bit of help on how to do this effectively in bash without a lot of extra looping or massive switch/case i have a long array of M elements and a short array of N elements, so M > N always. M is not a multiple of N. for case 1, I want to stretch N to fit M arrayHuge H = (... (2 Replies)
Discussion started by: f77hack
2 Replies

3. Shell Programming and Scripting

Pass array to a function and display the array

Hi All I have multiple arrays like below. set -A val1 1 2 4 5 set -A val2 a b c d . . . Now i would like to pass the individual arrays one by one to a function and display/ do some action. Note : I am using ksh Can you please advise any solution... Thanks in advance. (7 Replies)
Discussion started by: Girish19
7 Replies

4. Shell Programming and Scripting

Bash 3.2 - Array / Regex - IF 3rd member in array ends in 5 digits then do somthing...

Trying to do some control flow parsing based on the index postion of an array member. Here is the pseudo code I am trying to write in (preferably in pure bash) where possible. I am thinking regex with do the trick, but need a little help. pesudo code if == ENDSINFIVEINTS ]]; then do... (4 Replies)
Discussion started by: briandanielz
4 Replies

5. Shell Programming and Scripting

PERL : Read an array and write to another array with intial string pattern checks

I have an array and two variables as below, I need to check if $datevar is present in $filename. If so, i need to replace $filename with the values in the array. I need the output inside an ARRAY How can this be done. Any help will be appreciated. Thanks in advance. (2 Replies)
Discussion started by: irudayaraj
2 Replies

6. Shell Programming and Scripting

Store all the passed arguments in an array and display the array

Hi I want to write a script which store all the parameters passed to the script into an array. Once it is stored I want scan through the array and and delete those files for last month present inside the directory. The files in directory is appneded with YYYY_MM_DD. I want to know how can I... (3 Replies)
Discussion started by: dgmm
3 Replies

7. Shell Programming and Scripting

perl, put one array into many array when field is equal to sth

Hi Everyone, #!/usr/bin/perl use strict; use warnings; my @test=("a;b;qqq;c;d","a;b;ggg;c;d","a;b;qqq;c;d"); would like to split the @test array into two array: @test1=(("a;b;qqq;c;d","a;b;qqq;c;d"); and @test2=("a;b;ggg;c;d"); means search for 3rd filed. Thanks find the... (0 Replies)
Discussion started by: jimmy_y
0 Replies

8. Shell Programming and Scripting

PHP: Search Multi-Dimensional(nested) array and export values of currenly worked on array.

Hi All, I'm writing a nagios check that will see if our ldap servers are in sync... I got the status data into a nested array, I would like to search key of each array and if "OK" is NOT present, echo other key=>values in the current array to a variable so...eg...let take the single array... (1 Reply)
Discussion started by: zeekblack
1 Replies

9. Programming

Creating an array to hold posix thread ids: Only dynamic array works

I am facing a strange error while creating posix threads: Given below are two snippets of code, the first one works whereas the second one gives a garbage value in the output. Snippet 1 This works: -------------- int *threadids; threadids = (int *) malloc (num_threads * sizeof(int)); ... (4 Replies)
Discussion started by: kmehta
4 Replies

10. Shell Programming and Scripting

create array holding characters from sring then echo array.

Hi, I wish to store $string1 in $string1array a character in each array element. Then i wish to echo the entire array to the screen so that it reads as the normal string again. I have been trying with the code below but does not work. Please help... To put string into array: ... (5 Replies)
Discussion started by: rorey_breaker
5 Replies
Login or Register to Ask a Question