Bash Script Array index value
Is there any way to get the value that is in the array index. ie
array[1]=one array[2]=two array[3]=three That would be an array and I want the index values to be printed as well not just the value of the array. Thanks |
Something like :
Code:
#!/bin/bash http://linuxreviews.org/beginner/Bas.../en/x4945.html http://www.die.net/doc/linux/abs-guide/arrays.html |
Well what I forgot to mention was that I am using a wierd scheme for my index numbers. They are kind of like reference numbers, hence why i would want to know what they are so I can use what the refernce to in my scripts.
|
I think you will get better result with searching the index in the array
from a value reference like : Code:
#!/bin/bash |
All I really want to do is know if i can print the number. I will show you what i mean
I have a number sequence like server[101]=Server 1 server[201]=Server 1 server[301]=Server 1 server[401]=Server 1 server[501]=Server 1 Not in numeric order they would reference somehting like 101=Site1 201=site2 etc... I was wondering if i could just echo the index number so i can reference to it like if ${server[ID]} so i can say like Server 1 at Site1 Does this make sense at all??? |
Well you could echo just the index number with my function above,
just replace 'myArray' with 'server', although it won't work wiith arrays that contains multiple same values... Now if there are some built-in or shorter functions to echo the index of a bash array, I will like to see it |
Thank you for all your input, I know witch some other languages you can just output the index name. i wasn't sure if bash had that or not. Thanks again for your help.
|
Kedelfor,
were you able to find a solution to your question? I need to do the exact same thing, TIA |
Hello,
I found this construct to solve your problem: ${!array[*]} Code:
#!/bin/sh atelszewski |
not compatible with earlier versions
note that the syntax used by atelszewski did not exist in earlier versions of bash.
-- Poor Yorick www.pooryorick.com |
i think one needs to enable the extended pattern matching using
Code:
shopt -s extglob |
All times are GMT -5. The time now is 07:42 PM. |