ch9 arrays

Arrays in the shell are only one-dimensional

if you want 2 dimensional array - 64 , the alternative is to have 8 arrays of 8
-----------
New to bash version 4 are associative arrays. These arrays have text instead of a number
as their index, so you can keep track of race results using ${points[Ferrari]} and
${points[McLaren]} rather than ${points[0]} and ${points[1]} and then having a lookup
table mapping 0 to “Ferrari” and 1 to “McLaren.”
-----------
index number goes in square brackets
numberarray[0]=zero
numberarray[1]=one

-----------
$ cat studentarray.sh
#!/bin/bash
students=( Dave Jennifer Michael # year 1
Alistair Lucy Richard Elizabeth # year 2
Albert Roger Dennis James Roy # year 3
Rory Jim Andi Elaine Clive # year 4
)
for name in ${students[@]}
do
   echo -en “$name “
done
echo

$ ./studentarray.sh
Dave Jennifer Michael Alistair Lucy Richard Elizabeth Albert Roger Dennis James Roy
Rory Jim Andi Elaine Clive
$
--------------

$ mp3s=( *.mp3 )
$ for mp3 in “${mp3s[@]}
> do
> echo “MP3 File: $mp3”
> done

MP3 File: 01 - The MC5 - Kick Out The Jams.mp3
MP3 File: 02 - Velvet Underground - I’m Waiting For The Man.mp3
MP3 File: 03 - The Stooges - No Fun.mp3
MP3 File: 04 - The Doors - L.A. Woman.mp3
----------------
The bash shell builtin command readarray reads in text files in an even more flexible manner than
that shown in the preceding script for reading /etc/hosts. The initial index value can be specified
(-O), as can the

$ readarray -n 4 -s 2 food
porridge
black pudding
apples
bananas
cucumbers
burgers
eggs

$ printf “%s” “${food[@]}
apples
bananas
cucumbers
burgers

The first two items were skipped because of the -s 2 parameter. Only four actual parameters were
read because of the -n 4 parameter

One common way to display the items of an array is the printf “%s\n” “${food[@]}” notation;
this iterates through the values of the array, printing each as a string followed by a newline
-------------------
Accessing arrays

$ numberarray[0]=zero
$ numberarray[1]=one
$ numberarray[3]=three
$ echo ${numberarray[0]}
zero
$ echo ${numberarray[2]}
$ echo ${numberarray[3]}
three
$

If you try to access $numberarray[1] without the curly brackets, the shell will interpret
$numberarray as the first element within numberarray, and [1] as a literal string

$ echo $numberarray[1]
zero[1]
$


While ${#myvar} gives the length of the string contained in the $myvar variable, ${#myarray[@]}
or ${#myarray[*]} returns the number of elements in the array

$ fruits=( apple banana pear orange )
$ echo ${#fruits[@]}
4
$ echo ${#fruits}
5
$ echo ${#fruits[0]}
5
----------------
Selecting Items from Array
This is done in a substr-like way, providing the starting index and the number of items to
retrieve. A simple ${food[@]:0:1} gets the first item; :1:1 gets the second, :2:1 gets the third, and
so on. That is the same as using ${food[0]}, [1], and [2], and not apparently very useful.

$ food=( apples bananas cucumbers dates eggs fajitas grapes )
$ echo ${food[@]:0:1}
apples
$ echo ${food[@]:1:1}
bananas
$ echo ${food[@]:2:1}
cucumbers
$ echo ${food[@]:7:1}

$

--
By replacing the final :1 in the preceding
code with a different number, you can retrieve a set of results from the array.
$ echo ${food[@]:2:4}
cucumbers dates eggs fajitas
$ echo ${food[@]:0:3}
apples bananas cucumbers
$
---------------
Displaying the entire Array

To display the entire variable, a simple echo ${array[@]} will suffice, but it is not particularly
appealing:
$ echo ${distros[@]}
Ubuntu Fedora Debian openSuSE Sabayon Arch Puppy

-----------
Associative arrays
The associative array is a new feature in bash version 4. Associative arrays link (associate) the value and the index together, so you can associate metadata with the actual data. You can use this to associate a musician with his instrument.
An associative array must be declared as such with the uppercase
declare -A command.

$ cat musicians.sh
#!/bin/bash
declare -A beatles
beatles=( [singer]=John [bassist]=Paul [drummer]=Ringo [guitarist]=George )
for musician in singer bassist drummer guitarist
 do
    echo “The ${musician} is ${beatles[$musician]}.”
 done
$ ./musicians.sh
The singer is John.
The bassist is Paul.
The drummer is Ringo.
The guitarist is George.
$

---------
What makes associative arrays even more useful is the ability to reference back to the name of
the index.
To do this, use the
${!array[@]} syntax.

$ cat instruments.sh
#!/bin/bash
declare -A beatles
beatles=( [singer]=John [bassist]=Paul [drummer]=Ringo [guitarist]=George )
for instrument in ${!beatles[@]}
  do
    echo “The ${instrument} is ${beatles[$instrument]}
  done
$ ./instruments.sh
The singer is John
The guitarist is George
The bassist is Paul
The drummer is Ringo
$
-------------
Copying array

$ activities=( swimming “water skiing” canoeing “white-water rafting” surfing )
$ for act in ${activities[@]}
do
echo “Activity: $act”
done

Activity: swimming
Activity: water
Activity: skiing

Appending to Array
$ hobbies=( “${activities[@]” diving )
$ for hobby in “${hobbies[@]}”
do
echo “Hobby: $hobby”
done

Hobby: swimming
Hobby: water skiing
Hobby: canoeing
Hobby: white-water rafting
Hobby: surfing
Hobby: scuba diving
Hobby: diving

Deleting from Array
$ for act in `seq 0 $((${#activities[@]} - 1))`
do
echo “Activity $act: ${activities[$act]}”
done

$ activities=
$ for act in `seq 0 $((${#activities[@]} - 1))`
do
echo “Activity $act: ${activities[$act]}”
done


Comments

Popular posts from this blog

HAproxy logging

teamcity Automatic Agent Start under Linux

NFS mount add in fstab _netdev instead of default | firewall-cmd --list-all