Problem with arrays and loop


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Problem with arrays and loop
# 1  
Old 10-04-2011
Problem with arrays and loop

Hello ,
im sorry for my english .
im trying to create a dynamic menu that will display if the interface is ACTIVE OR STOPPED/FAILED

for some reason i cant get it to work properly
Code:
start_interface_func()
{
  i=0
for interface_chk in 11 71 73 72 12 47 48 49 50 20 23 24 25 46 21 22 27 28 29 403 62 45 60 13 402 401 4 44 26
   do 
     interface_state[$i]=`cat /tmp/oper/tmp_fnex_file_1 | grep $interface_chk | awk '{ print $2}'`
  i=$(( $i + 1 ))
   done
}
  start_interface_func
  while true 
  do
    clear
    echo "\n"
    echo "\n"
    echo "---------------- AAA --------------=---------------- BG ----------------"
    echo "                                   ="
    echo "1) AAA 1 -11- ${interface_state[0]}              =  5)  BG 1 -12- ${interface_state[4]}"
    echo "2) AAA 2 -71- ${interface_state[1]}              =  6)  BG 2 -47- ${interface_state[5]}"
    echo "3) AAA 2 -73- ${interface_state[2]}              =  7)  BG 3 -48- ${interface_state[6]}"
    echo "4) AAA 3 -72- ${interface_state[3]}              =  8)  BG 4 -49- ${interface_state[7]}"
    echo "                                   =  9) BG 5 -50- ${interface_state[8]}"
    echo "             ="
    echo "---------------- GHLR -------------=---------------- SAiHLR --------------"
    echo "                                   ="
    echo "10) TAP fasr port 1  -20- ${interface_state[9]}  = 20) DRP SAiHLR   -403- ${interface_state[19]}"
    echo "11) TAP fasr port 2  -23- ${interface_state[10]} = 21) NSA DAP DRP  -62-  ${interface_state[20]}"
    echo "12) TAP fasr port 3  -24- ${interface_state[11]} = 22) SAiHLR 1     -45-  ${interface_state[21]}"
    echo "13) TAP fasr port 4  -25- ${interface_state[12]} = 23) SAiHLR 2     -60-  ${interface_state[22]}"
    echo "14) TAP fasr port 5  -46- ${interface_state[13]} =---------------- PPS --------------"
    echo "15) TAP fasr port 6  -21- ${interface_state[14]} = 24) PPS 1    -13-  ${interface_state[23]}"
    echo "16) TAP fasr port 7  -22- ${interface_state[15]} = 25) PPS Bill -402- ${interface_state[24]}"
    echo "17) TAP fasr port 8  -27- ${interface_state[16]} =---------------- NDM --------------"
    echo "18) TAP fasr port 9  -28- ${interface_state[17]} = 26) NDM  -401- ${interface_state[25]}"
    echo "19) TAP fasr port 10 -29- ${interface_state[18]} ="
    echo "                                   ="
    echo "---------------- VMP --------------=---------------- 3PPG --------------"
    echo "27) VMP 10         -4-  ${interface_state[26]}    = 29) 3PPG  -26- ${interface_state[28]}"
    echo "28) VMP 2nd xface  -44- ${interface_state[27]}    ="
    echo "\n"
    echo "31) Start All GHLR Interfaces"
    echo "32) Stop All GHLR Interfaces"
    echo "33) Start All SAiHLR Interfaces"
    echo "34) Stop All SAiHLR Interfaces"
    echo "\n"
    echo "      35)                  Main Menu "
    echo "\n"
    echo "Enter your menu choice [1-35]: "
 read selchoice
    case $selchoice in
   1) read;;
   2) read;;
   3) read;;
 esac
done

and this is what i get :

Code:
---------------- AAA --------------=---------------- BG ----------------
                                   =
1) AAA 1 -11- ACTIVE              =  5)  BG 1 -12- ACTIVE
2) AAA 2 -71- FAILED              =  6)  BG 2 -47- ACTIVE
3) AAA 2 -73- ACTIVE              =  7)  BG 3 -48- ACTIVE
4) AAA 3 -72- ACTIVE              =  8)  BG 4 -49- ACTIVE
                                   =  9) BG 5 -50- ACTIVE
                                                                     =
---------------- GHLR -------------=---------------- SAiHLR --------------
                                   =
10) TAP fasr port 1  -20- ACTIVE  = 20) DRP SAiHLR   -403- ACTIVE
11) TAP fasr port 2  -23- ACTIVE = 21) NSA DAP DRP  -62-  ACTIVE
12) TAP fasr port 3  -24- ACTIVE = 22) SAiHLR 1     -45-  ACTIVE
13) TAP fasr port 4  -25- ACTIVE = 23) SAiHLR 2     -60-  ACTIVE
14) TAP fasr port 5  -46- ACTIVE =---------------- PPS --------------
15) TAP fasr port 6  -21- ACTIVE = 24) PPS 1    -13-  ACTIVE
16) TAP fasr port 7  -22- ACTIVE = 25) PPS Bill -402- ACTIVE
17) TAP fasr port 8  -27- ACTIVE =---------------- NDM --------------
18) TAP fasr port 9  -28- ACTIVE = 26) NDM  -401- ACTIVE
19) TAP fasr port 10 -29- ACTIVE =
                                   =
---------------- VMP --------------=---------------- 3PPG --------------
27) VMP 10         -4-  ACTIVE
ACTIVE
ACTIVE
ACTIVE
ACTIVE
ACTIVE
ACTIVE
ACTIVE
ACTIVE
ACTIVE
ACTIVE
FAILED    = 29) 3PPG  -26- ACTIVE
28) VMP 2nd xface  -44- ACTIVE    =
 
31) Start All GHLR Interfaces
32) Stop All GHLR Interfaces
33) Start All SAiHLR Interfaces
34) Stop All SAiHLR Interfaces
 
      35)                  Main Menu 
 
Enter your menu choice [1-35]:

Thank You
# 2  
Old 10-04-2011
can you write contents of this file?
Code:
/tmp/oper/tmp_fnex_file_1

This User Gave Thanks to ygemici For This Post:
# 3  
Old 10-04-2011
sure ,
/tmp/oper/tmp_fnex_file_1
Code:
cat /tmp/oper/tmp_fnex_file_1
62:     ACTIVE  SAiHLR  NSA DAP DRP support in Restore
401:    ACTIVE  NDM     NDM
71:     FAILED  AAA     AAA 2
73:     ACTIVE  AAA     AAA 2
47:     ACTIVE  BG      BG 2
23:     ACTIVE  GHLR    TAP fast port 2
26:     ACTIVE  3PPG    3PPG
11:     ACTIVE  AAA     AAA 1
50:     ACTIVE  BG      BG 5
48:     ACTIVE  BG      BG 3
49:     ACTIVE  BG      BG 4
12:     ACTIVE  BG      BG 1
27:     ACTIVE  GHLR    TAP fast port 8
44:     ACTIVE  VMP     VMP 2nd xface
22:     ACTIVE  GHLR    TAP fast port 7
45:     ACTIVE  SAiHLR  SAiHLR 1
13:     ACTIVE  PPS     PPS 1
403:    ACTIVE  SAiHLR  DRP SAiHLR
28:     ACTIVE  GHLR    TAP fast port 9
21:     ACTIVE  GHLR    TAP fasr port 6
60:     ACTIVE  SAiHLR  SAiHLR 2
20:     ACTIVE  GHLR    TAP fast port 1
29:     ACTIVE  GHLR    TAP fast port 10
46:     ACTIVE  GHLR    TAP fast port 5
24:     ACTIVE  GHLR    TAP fast port 3
25:     ACTIVE  GHLR    TAP fast port 4
72:     ACTIVE  AAA     AAA 3
402:    ACTIVE  PPS     PPS Bill - Balance credit and debit
4:      FAILED  VMP     VMP 10

# 4  
Old 10-04-2011
Try this change...
Code:
...
interface_state[$i]=`grep $interface_chk: /tmp/oper/tmp_fnex_file_1| awk '{ print $2}'`
...

--ahamed

---------- Post updated at 12:22 AM ---------- Previous update was at 12:20 AM ----------

And you may want to use echo -e "\n" for newlines or simply use echo.

--ahamed
This User Gave Thanks to ahamed101 For This Post:
# 5  
Old 10-04-2011
Thank You very much !!!! solved my problem Smilie
# 6  
Old 10-04-2011
Quote:
Originally Posted by visiown
sure ,
/tmp/oper/tmp_fnex_file_1
Code:
cat /tmp/oper/tmp_fnex_file_1
62:     ACTIVE  SAiHLR  NSA DAP DRP support in Restore
401:    ACTIVE  NDM     NDM
71:     FAILED  AAA     AAA 2
73:     ACTIVE  AAA     AAA 2
47:     ACTIVE  BG      BG 2
23:     ACTIVE  GHLR    TAP fast port 2
26:     ACTIVE  3PPG    3PPG
11:     ACTIVE  AAA     AAA 1
50:     ACTIVE  BG      BG 5
48:     ACTIVE  BG      BG 3
49:     ACTIVE  BG      BG 4
12:     ACTIVE  BG      BG 1
27:     ACTIVE  GHLR    TAP fast port 8
44:     ACTIVE  VMP     VMP 2nd xface
22:     ACTIVE  GHLR    TAP fast port 7
45:     ACTIVE  SAiHLR  SAiHLR 1
13:     ACTIVE  PPS     PPS 1
403:    ACTIVE  SAiHLR  DRP SAiHLR
28:     ACTIVE  GHLR    TAP fast port 9
21:     ACTIVE  GHLR    TAP fasr port 6
60:     ACTIVE  SAiHLR  SAiHLR 2
20:     ACTIVE  GHLR    TAP fast port 1
29:     ACTIVE  GHLR    TAP fast port 10
46:     ACTIVE  GHLR    TAP fast port 5
24:     ACTIVE  GHLR    TAP fast port 3
25:     ACTIVE  GHLR    TAP fast port 4
72:     ACTIVE  AAA     AAA 3
402:    ACTIVE  PPS     PPS Bill - Balance credit and debit
4:      FAILED  VMP     VMP 10

Code:
#!/bin/bash
sort -k3 /tmp/oper/tmp_fnex_file_1 > /tmp/oper/.tmp_tmp_fnex_file_1
awk -v w=$1 'BEGIN{printf "%+60s\n%+60s\n%+60s\n\n%s\n%s\n","    *************************************",
"-- Welcome to Interface Check Menu --","  *************************************",
"Now viewing all interfaces...","  *************************************************"};
{system("sleep 0.1");$2="";$1=substr($1,1,length($1-1));}{if(NR%w){printf("%s%s%s\n",NR,"-> ",$0)}
else{printf "%d%s%s\n%s\n",NR,"-> ",$0,"  *************************************************"}};
END{printf "%s","  *************************************************"
}' /tmp/oper/.tmp_tmp_fnex_file_1
N=$(($1-1));NN=$(wc -l</tmp/oper/tmp_fnex_file_1)

tmpcase='_case_m_f'
echo -e 'case $c in'>$tmpcase
xx=1;f=0;x=c
echo "[$xx-$N])c=\$$x;;">>$tmpcase
for((i=1;i<$NN-$N;i+=$N+1));do
#x='$(($c-'$xx'))'
echo "[$xx][$f-$N])c=\$$x;;">>$tmpcase;((xx++))
done
l=$(echo $NN|sed 's/.*\(.\)$/\1/')
echo "[$xx][$f-$l])c=\$$x;;">>$tmpcase
echo -e 'q|Q|[a-z]*|[A-Z]*)echo "quitting..";break;;
*)echo "invalid choice!!"\nesac'>>$tmpcase
if ! [ -x _$tmpcase ] ; then
chmod u+x $tmpcase ; fi

while :; do
echo -e "\n ================================="
read -p "   Enter your menu choice [1-$NN]:..? " c
. $tmpcase
awk 'NR=='$c' {print $1 "numbered device is-> " $2"!!"}' /tmp/oper/.tmp_tmp_fnex_file_1
done

Code:
# ./justdoit 10
                       *************************************
                       -- Welcome to Interface Check Menu --
                       *************************************

Now viewing all interfaces...
  *************************************************
1-> 26  3PPG 3PPG
2-> 11  AAA AAA 1
3-> 71  AAA AAA 2
4-> 73  AAA AAA 2
5-> 72  AAA AAA 3
6-> 12  BG BG 1
7-> 47  BG BG 2
8-> 48  BG BG 3
9-> 49  BG BG 4
10-> 50  BG BG 5
  *************************************************
11-> 21  GHLR TAP fasr port 6
12-> 20  GHLR TAP fast port 1
13-> 29  GHLR TAP fast port 10
14-> 23  GHLR TAP fast port 2
15-> 24  GHLR TAP fast port 3
16-> 25  GHLR TAP fast port 4
17-> 46  GHLR TAP fast port 5
18-> 22  GHLR TAP fast port 7
19-> 27  GHLR TAP fast port 8
20-> 28  GHLR TAP fast port 9
  *************************************************
21-> 401  NDM NDM
22-> 13  PPS PPS 1
23-> 402  PPS PPS Bill - Balance credit and debit
24-> 403  SAiHLR DRP SAiHLR
25-> 62  SAiHLR NSA DAP DRP support in Restore
26-> 45  SAiHLR SAiHLR 1
27-> 60  SAiHLR SAiHLR 2
28-> 4  VMP VMP 10
29-> 44  VMP VMP 2nd xface
30-> 4444  VMP VMP 4444
  *************************************************
  *************************************************
 =================================
   Enter your menu choice [1-30]:..? 34
invalid choice!!

 =================================
   Enter your menu choice [1-30]:..? 29
44:numbered device is-> ACTIVE!!

 =================================
   Enter your menu choice [1-30]:..? q
quitting..


regards
ygemici

Last edited by ygemici; 10-05-2011 at 05:48 AM..
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Multiple arrays in variable using for loop

Hi, I'm trying to get the number of files inside same kind of folders on each disks and assigning each values in to a variable named with same folder and disk name so that it'll be easy for me to identify each time.But somehow I'm not able to assign those values in that specific name variable... (1 Reply)
Discussion started by: ratheeshp
1 Replies

2. Shell Programming and Scripting

Loop over multiple arrays

Hi All I need really really help with this :- I have two files ( File1 , File 2) both files are output of two different scripts. File1 usually has a list of names ( sometimes 3 names sometimes 5 sometimes more , depends about the output of the script) File2 usually has a list of numbers... (2 Replies)
Discussion started by: samsan
2 Replies

3. Shell Programming and Scripting

Bash for loop with arrays second variable?

I am fairly new to bash and am not sure how to resolve this: I have a series of geographical long/lat points eg. 50/-30 listed on separate lines in a file called junk2. I have input these into an array and am then using that array in a for loop. Towards the end of the loop I create a file called... (4 Replies)
Discussion started by: lily-anne
4 Replies

4. Shell Programming and Scripting

Loop through array of arrays of string with spaces

Hi I'm trying to loop through an array that contains other arrays and these arrays consist of strings with spaces. The problem is that I can't seem to preserve the spacing in the string. The string with spaces are either divided into multiple items if I change IFS to \n or all the elements of... (4 Replies)
Discussion started by: kidmanos
4 Replies

5. Shell Programming and Scripting

Iterating through two arrays in a single loop

Hey everyone. Is it possible to use two arrays in a loop? Basically what I am trying to do is iterate through the elements in an array, and, based on a condition, use the current position in the array to point to that index in the array. Here's the loop structure I'm looking for: ... (1 Reply)
Discussion started by: msarro
1 Replies

6. Shell Programming and Scripting

does bash have arrays that i can push into and run a for loop against?

Hi I have a bash script where i need to push some values into an array and when finished, run a for loop against that array for example myfile sausages|meat beef| meat carrot| veg ... ... for LINE in `cat myfile`; do FOOD=`echo $LINE | cut -d\| -f1` TYPE=`echo $LINE | cut... (4 Replies)
Discussion started by: rethink
4 Replies

7. Programming

Perl arrays and loop through array

Hi All I need to get <STDIN> from a user. the <STDIN> is a range of number delimited by "," (comma) and can be with range delimited by "-". Example: 1,2,3,4-9,12,15,34-36,70 Now I need to get this from the user and go on each number and "Do something"... but when trying to do this as above... (2 Replies)
Discussion started by: RedGrinGo
2 Replies

8. Shell Programming and Scripting

How to access the elements of two arrays with a single loop using the inbuilt index.

Hi all, I wanted to access two arrays (of same size) using one for loop. Ex: #!/bin/bash declare -a num declare -a words num=(1 2 3 4 5 6 7) words=(one two three four five six seven) for num in ${num} do echo ":$num: :${words}:" done Required Output: :1: :one: (11 Replies)
Discussion started by: 14341
11 Replies

9. Shell Programming and Scripting

Using arrays outside the loop

set -A town_name india pakistan srilanka india set -A town m=0 n=0 while } ] do t1=`echo ${town_name}` town= $t1 echo ${town} n=$((n+1)) m=$((m+1)) done t2=`echo ${town}` echo $t2 i m trying to get the value of town array outside the loop but i m nt getting it.. Could u plz... (5 Replies)
Discussion started by: priyanka3006
5 Replies

10. Shell Programming and Scripting

Nested Loop to Echo Multiple Arrays

I have three arrays which hold three elements each. I have a fourth array which contains the names of those three arrays. I'm having difficulty creating a nested loop that can loop through each array and echo their values. script #!/bin/ksh # array of locations (usa, london, australia)... (1 Reply)
Discussion started by: yongho
1 Replies
Login or Register to Ask a Question