You can define array as follows:
set -A arrayName value1 value2 value3
For example, create an array called characters with three values as follows:set -A characters Mugen Jin Fuu
To print first value, enter:echo ${characters[0]}To print 3rd and last value, enter:
echo ${characters[2]}To print all values, enter:
echo ${characters[@]}To count number of items in an array called characters, enter:
echo ${#characters[@]}You can use for loop as follows to iterate through all values:
for i in ${characters[@]}; do echo "Samurai Champloo character - $i"; doneSample outputs:
Samurai Champloo character - MugenYou can add two more items as follows to exiting array:
Samurai Champloo character - Jin
Samurai Champloo character - Fuu
characters[3]="Sunflower-Samurai"
characters[4]="Detective-Manzo"
Sample Shell Script
#!/bin/ksh
# set array called nameservers
set -A nameservers 192.168.1.1 192.168.1.5 202.54.1.5
# print all name servers
for i in ${nameservers[@]
do
echo $i
done
No comments:
Post a Comment