Matrices en Unix Bourne Shell

Pregunta:

Estoy tratando de usar matrices en Bourne shell ( /bin/sh ). Descubrí que la forma de inicializar elementos de matriz es:

arr=(1 2 3)

Pero encuentra un error:

syntax error at line 8: `arr=' unexpected

Ahora, la publicación donde encontré esta sintaxis dice que es para bash , pero no pude encontrar ninguna sintaxis separada para Bourne shell. ¿La sintaxis también es la misma para /bin/sh ?

Respuesta:

/bin/sh casi nunca es un shell Bourne en ningún sistema hoy en día (incluso Solaris, que fue uno de los últimos sistemas importantes en incluirlo, ahora ha cambiado a POSIX sh para su / bin / sh en Solaris 11). /bin/sh fue el caparazón de Thompson a principios de los 70. El shell Bourne lo reemplazó en Unix V7 en 1979.

/bin/sh ha sido el shell de Bourne durante muchos años a partir de entonces (o el shell de Almquist, una reimplementación gratuita en BSD).

Hoy en día, /bin/sh es más comúnmente un intérprete u otro para el lenguaje POSIX sh , que a su vez se basa en un subconjunto del lenguaje ksh88 (y un superconjunto del lenguaje shell Bourne con algunas incompatibilidades).

El shell Bourne o la especificación del lenguaje POSIX sh no admiten matrices. O más bien, tienen solo una matriz: los parámetros posicionales ( $1 , $2 , $@ , por lo que también una matriz por función).

ksh88 tenía matrices que estableciste con el set -A , pero eso no se especificó en el POSIX sh ya que la sintaxis es incómoda y no muy utilizable.

Otros shells con variables de matriz / listas incluyen: csh / tcsh , rc , es , bash (que en su mayoría copió la sintaxis ksh de la manera ksh93), yash , zsh , fish cada uno con una sintaxis diferente ( rc el shell del que alguna vez fue sucesor de Unix, siendo fish y zsh los más consistentes) …

En sh estándar (también funciona en versiones modernas del shell Bourne):

set '1st element' 2 3 # setting the array

set -- "$@" more # adding elements to the end of the array

shift 2 # removing elements (here 2) from the beginning of the array

printf '<%s>\n' "$@" # passing all the elements of the $@ array 
                     # as arguments to a command

for i do # looping over the  elements of the $@ array ($1, $2...)
  printf 'Looping over "%s"\n' "$i"
done

printf '%s\n' "$1" # accessing individual element of the array.
                   # up to the 9th only with the Bourne shell though
                   # (only the Bourne shell), and note that you need
                   # the braces (as in "${10}") past the 9th in other
                   # shells (except zsh, when not in sh emulation and
                   # most ash-based shells).

printf '%s\n' "$# elements in the array"

printf '%s\n' "$*" # join the elements of the array with the 
                   # first character (byte in some implementations)
                   # of $IFS (not in the Bourne shell where it's on
                   # space instead regardless of the value of $IFS)

(tenga en cuenta que en el shell Bourne y ksh88, $IFS debe contener el carácter de espacio para que "$@" funcione correctamente (un error), y en el shell Bourne, no puede acceder a elementos por encima de $9 ( ${10} ganó no funciona, todavía puedes hacer el shift 1; echo "$9" haz shift 1; echo "$9" o repite sobre ellos)).

Leave a Comment

Your email address will not be published. Required fields are marked *

web tasarım