Simpler Navigation for Servers and Operating Systems - Please Update Your Bookmarks
Completed: a much simpler Servers and Operating Systems section of the Community. We combined many of the older boards, so you won't have to click through so many levels to get at the information you need. Check the consolidated boards here as many sub-forums are now single boards.
If you have bookmarked forums or discussion boards in Servers and Operating Systems, we suggest you check and update them as needed.
cancel
Showing results for 
Search instead for 
Did you mean: 

shell script

SOLVED
Go to solution
tarek_3
Frequent Advisor

shell script

hi all
is there aby way to use array in shell script to save variables
thanks
5 REPLIES
Yogeeraj_1
Honored Contributor
Solution

Re: shell script

 
No person was ever honoured for what he received. Honour has been the reward for what he gave (clavin coolidge)
Duncan Edmonstone
Honored Contributor

Re: shell script

yes, but only one dimensional arrays.

e.g. in korn/posix

i[0]="foo"
i[1]="bar"
i[2]="baz"

for x in 0 1 2
do
echo ${i[${x}]}
done

HTH

Duncan

HTH

Duncan
Ricky B. Nino
Frequent Advisor

Re: shell script

Hi Tarek,

Yes you can...

Like in any other programming languages Arrays in Korn SHELL are variables that contain more than one value.

example...

integer my_array
my_array[0]=5
my_array[1]=16

The first element of an array is indexed by 0, the second element is 1, and so on. The largest index value that is valid for array is 1023.

You must enclose an array reference in braces for the korn shell to recognize it as such.

For example...
${my_array[1]}

Instead of...
$my_array[1]

The syntax for accessing the value of the nth array element is, where n is an integer... {my_array[n]}

To print the values of all array elements, use the syntax: {my_array[*]}

To print the actual number of elements (assigned values) in the array use the syntax: {#array_name[*]}

Array elements do not have to be assigned in order. Moreover, you can skip the assignment of values to any elements you want. For instance....

my_array[2]=2
my_array[5]=3
my_array[1023]=1

Array elements that are skipped over are not assigned a value and, effectively, are treated as though they do not exist.

Hope this helps...

best regards...
Opportunities expand for people willing to put time and effort into learning new skills.
Ramkumar Devanathan
Honored Contributor

Re: shell script

hi,

In k-shell use set with -A option to declare an array and read output of a command into the array in one step.

$ set -A users `awk -F: '{print $1}' /etc/passwd`

$ echo ${users[0]}
root

hope this helps.

- ramd.
HPE Software Rocks!
Zafar A. Mohammed_1
Trusted Contributor

Re: shell script

Hi Tarek,

Use this URL documentation for compelete reference also:
http://docs.hp.com/hpux/pdf/B2355-90046.pdf

Thanks
Zafar