*. Aca vamos a ver como utilizar arrays.*
Ejemplos:
Declarando Array:
nombre[indice]=valor
* Nombre es cualquier nombre para un array.
* Indice puede ser cualquier numero o expresion que me devuelva un numero mayor que cero.
Ejemplo 1:
$ cat arrayej1.sh
#! /bin/bash
Unix[0]='Debian'
Unix[1]='Red hat'
Unix[2]='Ubuntu'
Unix[3]='Suse'
echo ${Unix[1]}
$./arrayej1.sh
Red hat
Inicializando un array por declaracion:
Sintaxis:
declare -a nombrearray=(elemento1 elemento2 elemento3)
$cat arrayej2.sh
#! /bin/bash
declare -a Presi=('Peron' 'Alfonsin' 'Menem' 'De La Rua' 'Kirchner');
Imprimir un array:
$cat arrayej2.sh
#! /bin/bash
declare -a Presi=('Peron' 'Alfonsin' 'Menem' 'De La Rua' 'Kirchner');
echo ${Presi[@]}
#./arrayej2.sh
Peron Alfonsin Menem De La Rua Kirchner
Tamaño de un array:
${#nombrearray[@]} --> te devuelve la cantidad elementos
$cat arrayej2.sh
#! /bin/bash
declare -a Presi=('Peron' 'Alfonsin' 'Menem' 'De La Rua' 'Kirchner');
echo ${#Unix[@]} #Numeros de elementos en el array
echo ${#Unix} #Numeros de caracteres en el primer elemento del array como por ejemplo Peron
$./arraymanip.sh
4
5
Tamaño del elemento n del array
${#nombrearray[n]}
$cat arrayej2.sh
#! /bin/bash
Presi[0]='Peron'
Presi[1]='Alfonsin'
Presi[2]='Menem'
Presi[3]='Kirchner'
echo ${#Presi[3]} # lTamaño del elemento situadio en la posicion
$./arraymanip.sh
8
Extraer por el offset y tamaño del array.
Aca vemos como extraemos dos elementos desde la posicion 3.
$cat arraymanip.sh
Unix=('Debian' 'Red hat' 'Ubuntu' 'Suse' 'Fedora' 'UTS' 'OpenLinux');
echo ${Unix[@]:3:2}
$./arraymanip.sh
Suse Fedora
Extraer desde el offset y el tamaño un elemento en particular del array
$cat arraymanip.sh
Aca vemos como desde la segunda posicion del array sacamos los 4 primeros elementos.
#! /bin/bash
Unix=('Debian' 'Red hat' 'Ubuntu' 'Suse' 'Fedora' 'UTS' 'OpenLinux');
echo ${Unix[2]:0:4}
./arraymanip.sh
Ubun
Aca vamos a utilizar busqueda y reemplazo
$cat arraymanip.sh
#!/bin/bash
Unix=('Debian' 'Red hat' 'Ubuntu' 'Suse' 'Fedora' 'UTS' 'OpenLinux');
echo ${Unix[@]/Ubuntu/SCO Unix}
$./arraymanip.sh
Debian Red hat SCO Unix Suse Fedora UTS OpenLinux
Aca reemplazamos el 2 elemento del array Ubuntu por SCO, pero esto no quedo definitivamente grabado en el array.
Agregar un elemento a un array existente.
$cat arraymanip.sh
Unix=('Debian' 'Red hat' 'Ubuntu' 'Suse' 'Fedora' 'UTS' 'OpenLinux');
Unix=("${Unix[@]}" "AIX" "HP-UX")
echo ${Unix[7]}
$./arraymanip.sh
AIX
Borrar un elemento del array
Una Forma:
$cat arraymanip.sh
#!/bin/bash
Unix=('Debian' 'Red hat' 'Ubuntu' 'Suse' 'Fedora' 'UTS' 'OpenLinux');
unset Unix[3]
echo ${Unix[3]}
Otra Forma:
$ cat arraymanip.sh
Unix=('Debian' 'Red hat' 'Ubuntu' 'Suse' 'Fedora' 'UTS' 'OpenLinux');
pos=3
Unix=(${Unix[@]:0:$pos} ${Unix[@]:$(($pos + 1))})
echo ${Unix[@]}
$./arraymanip.sh
Debian Red hat Ubuntu Fedora UTS OpenLinux
Remover elementos de un array utilizando patrones
$ cat arraymanip.sh
#!/bin/bash
declare -a Unix=('Debian' 'Red hat' 'Ubuntu' 'Suse' 'Fedora');
declare -a patter=( ${Unix[@]/Red*/} )
echo ${patter[@]}
$ ./arraymanip.sh
Debian Ubuntu Suse Fedora
Copiando un Array
#!/bin/bash
Unix=('Debian' 'Red hat' 'Ubuntu' 'Suse' 'Fedora' 'UTS' 'OpenLinux');
Linux=("${Unix[@]}")
echo ${Linux[@]}
$ ./arraymanip.sh
Debian Red hat Ubuntu Fedora UTS OpenLinux
Concatenar dos array
$cat arraymanip.sh
#!/bin/bash
Unix=('Debian' 'Red hat' 'Ubuntu' 'Suse' 'Fedora' 'UTS' 'OpenLinux');
Shell=('bash' 'csh' 'jsh' 'rsh' 'ksh' 'rc' 'tcsh');
UnixShell=("${Unix[@]}" "${Shell[@]}")
echo ${UnixShell[@]}
echo ${#UnixShell[@]}
$ ./arraymanip.sh
Debian Red hat Ubuntu Suse Fedora UTS OpenLinux bash csh jsh rsh ksh rc tcsh
14
Borrar un array entero:
$cat arraymanip.sh
#!/bin/bash
Unix=('Debian' 'Red hat' 'Ubuntu' 'Suse' 'Fedora' 'UTS' 'OpenLinux');
Shell=('bash' 'csh' 'jsh' 'rsh' 'ksh' 'rc' 'tcsh');
UnixShell=("${Unix[@]}" "${Shell[@]}")
unset UnixShell
echo ${#UnixShell[@]}
$ ./arraymanip.sh
0
Cargar el contenido de un archivo en un array
#Example file
$ cat logfile
Welcome
to
thegeekstuff
Linux
Unix
$ cat loadcontent.sh
#!/bin/bash
filecontent=( `cat "logfile" `)
for t in "${filecontent[@]}"
do
echo $t
done
echo "Read file content!"
$ ./loadcontent.sh
Welcome
to
thegeekstuff
Linux
Unix
Read file content!
Fuente:http://www.thegeekstuff.com/2010/06/bash-array-tutorial/
http://www.thegeekstuff.com/2010/06/bash-array-tutorial/

The Utilizando arrays by ITRestauracion, unless otherwise expressly stated, is licensed under a Creative Commons Attribution-NonCommercial-ShareAlike 3.0 Unported License.
Wow this is a great resource.. I’m enjoying it.. good article