Matrizes em Unix Bourne Shell

Matrizes em Unix Bourne Shell

Estou tentando usar matrizes no shell Bourne ( /bin/sh). Descobri que a maneira de inicializar os elementos do array é:

arr=(1 2 3)

Mas está encontrando um erro:

syntax error at line 8: `arr=' unexpected

Agora, o post onde encontrei essa sintaxe diz que é para bash, mas não consegui encontrar nenhuma sintaxe separada para o Bourne Shell. A sintaxe /bin/shtambém é a mesma ?

Responder1

/bin/shdificilmente é um shell Bourne em qualquer sistema hoje em dia (mesmo o Solaris, que foi um dos últimos grandes sistemas a incluí-lo, agora mudou para um sh POSIX para seu /bin/sh no Solaris 11). /bin/shfoi a concha Thompson no início dos anos 70. O shell Bourne o substituiu no Unix V7 em 1979.

/bin/shtem sido o shell Bourne por muitos anos (ou o shell Almquist, uma reimplementação gratuita em BSDs).

Hoje em dia, /bin/shé mais comumente um intérprete ou outro para a linguagem POSIX shque é baseada em um subconjunto da linguagem ksh88 (e um superconjunto da linguagem Bourne Shell com algumas incompatibilidades).

O shell Bourne ou a especificação da linguagem POSIX sh não suportam matrizes. Ou melhor, eles têm apenas um array: os parâmetros posicionais ( $1, $2, $@, portanto também um array por função).

ksh88 tinha arrays que você configurou com set -A, mas isso não foi especificado no POSIX sh porque a sintaxe é estranha e pouco utilizável.

Outros shells com variáveis ​​​​de array/listas incluem: csh/ tcsh, rc, es, bash(que em sua maioria copiou a sintaxe ksh da maneira ksh93), yash, zsh, fishcada um com uma sintaxe diferente ( rco shell do futuro sucessor do Unix, fishe zshsendo o mais consistente uns)...

No padrão sh(também funciona em versões modernas do 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)

(observe que no shell Bourne e no ksh88, $IFSdeve conter o caractere de espaço para "$@"funcionar corretamente (um bug), e no shell Bourne, você não pode acessar os elementos acima $9( ${10}não funcionará, você ainda pode fazer shift 1; echo "$9"ou repetir eles)).

Responder2

Como os outros disseram, o Bourne Shell não temverdadeiromatrizes.

No entanto, dependendo do que você precisa fazer, strings delimitadas devem ser suficientes:

sentence="I don't need arrays because I can use delimited strings"
for word in $sentence
do
  printf '%s\n' "$word"
done

Se os delimitadores típicos (espaço, tabulação e nova linha) não forem suficientes, você poderá definirIFSpara qualquer delimitador desejado antes do loop.

E se você precisar construir o array programaticamente, basta construir uma string delimitada.

Responder3

Não há matrizes no shell Bourne simples. Você pode usar a seguinte maneira para criar um array e percorrê-lo:

#!/bin/sh
# ARRAY.sh: example usage of arrays in Bourne Shell

array_traverse()
{
    for i in $(seq 1 $2)
    do
    current_value=$1$i
    echo $(eval echo \$$current_value)
    done
    return 1
}

ARRAY_1=one
ARRAY_2=two
ARRAY_3=333
array_traverse ARRAY_ 3

Não importa a maneira de usar arrays shque você escolher, sempre será complicado. Considere usar uma linguagem diferente, como Pythonou Perlse puder, a menos que esteja preso a uma plataforma muito limitada ou queira aprender alguma coisa.

Responder4

Uma forma de simular arrays em traço (pode ser adaptado para qualquer número de dimensões de um array): (Observe que o uso do seqcomando requer que IFSesteja definido como ' ' (ESPAÇO = valor padrão). Você pode usar while ... do ...ou do ... while ...em vez disso, faça loops para evitar isso (mantive seqo escopo de uma ilustração melhor do que o código faz).)

#!/bin/sh

## The following functions implement vectors (arrays) operations in dash:
## Definition of a vector <v>:
##      v_0 - variable that stores the number of elements of the vector
##      v_1..v_n, where n=v_0 - variables that store the values of the vector elements

VectorAddElementNext () {
# Vector Add Element Next
# Adds the string contained in variable $2 in the next element position (vector length + 1) in vector $1

    local elem_value
    local vector_length
    local elem_name

    eval elem_value=\"\$$2\"
    eval vector_length=\$$1\_0
    if [ -z "$vector_length" ]; then
        vector_length=$((0))
    fi

    vector_length=$(( vector_length + 1 ))
    elem_name=$1_$vector_length

    eval $elem_name=\"\$elem_value\"
    eval $1_0=$vector_length
}

VectorAddElementDVNext () {
# Vector Add Element Direct Value Next
# Adds the string $2 in the next element position (vector length + 1) in vector $1

    local elem_value
    local vector_length
    local elem_name

    eval elem_value="$2"
    eval vector_length=\$$1\_0
    if [ -z "$vector_length" ]; then
        vector_length=$((0))
    fi

    vector_length=$(( vector_length + 1 ))
    elem_name=$1_$vector_length

    eval $elem_name=\"\$elem_value\"
    eval $1_0=$vector_length
}

VectorAddElement () {
# Vector Add Element
# Adds the string contained in the variable $3 in the position contained in $2 (variable or direct value) in the vector $1

    local elem_value
    local elem_position
    local vector_length
    local elem_name

    eval elem_value=\"\$$3\"
    elem_position=$(($2))
    eval vector_length=\$$1\_0
    if [ -z "$vector_length" ]; then
        vector_length=$((0))
    fi

    if [ $elem_position -ge $vector_length ]; then
        vector_length=$elem_position
    fi

    elem_name=$1_$elem_position

    eval $elem_name=\"\$elem_value\"
    if [ ! $elem_position -eq 0 ]; then
        eval $1_0=$vector_length
    fi
}

VectorAddElementDV () {
# Vector Add Element
# Adds the string $3 in the position $2 (variable or direct value) in the vector $1

    local elem_value
    local elem_position
    local vector_length
    local elem_name

    eval elem_value="$3"
    elem_position=$(($2))
    eval vector_length=\$$1\_0
    if [ -z "$vector_length" ]; then
        vector_length=$((0))
    fi

    if [ $elem_position -ge $vector_length ]; then
        vector_length=$elem_position
    fi

    elem_name=$1_$elem_position

    eval $elem_name=\"\$elem_value\"
    if [ ! $elem_position -eq 0 ]; then
        eval $1_0=$vector_length
    fi
}

VectorPrint () {
# Vector Print
# Prints all the elements names and values of the vector $1 on sepparate lines

    local vector_length

    vector_length=$(($1_0))
    if [ "$vector_length" = "0" ]; then
        echo "Vector \"$1\" is empty!"
    else
        echo "Vector \"$1\":"
        for i in $(seq 1 $vector_length); do
            eval echo \"[$i]: \\\"\$$1\_$i\\\"\"
            ###OR: eval printf \'\%s\\\n\' \"[\$i]: \\\"\$$1\_$i\\\"\"
        done
    fi
}

VectorDestroy () {
# Vector Destroy
# Empties all the elements values of the vector $1

    local vector_length

    vector_length=$(($1_0))
    if [ ! "$vector_length" = "0" ]; then
        for i in $(seq 1 $vector_length); do
            unset $1_$i
        done
        unset $1_0
    fi
}

##################
### MAIN START ###
##################

## Setting vector 'params' with all the parameters received by the script:
for i in $(seq 1 $#); do
    eval param="\${$i}"
    VectorAddElementNext params param
done

# Printing the vector 'params':
VectorPrint params

read temp

## Setting vector 'params2' with the elements of the vector 'params' in reversed order:
if [ -n "$params_0" ]; then
    for i in $(seq 1 $params_0); do
        count=$((params_0-i+1))
        VectorAddElement params2 count params_$i
    done
fi

# Printing the vector 'params2':
VectorPrint params2

read temp

## Getting the values of 'params2'`s elements and printing them:
if [ -n "$params2_0" ]; then
    echo "Printing the elements of the vector 'params2':"
    for i in $(seq 1 $params2_0); do
        eval current_elem_value=\"\$params2\_$i\"
        echo "params2_$i=\"$current_elem_value\""
    done
else
    echo "Vector 'params2' is empty!"
fi

read temp

## Creating a two dimensional array ('a'):
for i in $(seq 1 10); do
    VectorAddElement a 0 i
    for j in $(seq 1 8); do
        value=$(( 8 * ( i - 1 ) + j ))
        VectorAddElementDV a_$i $j $value
    done
done

## Manually printing the two dimensional array ('a'):
echo "Printing the two-dimensional array 'a':"
if [ -n "$a_0" ]; then
    for i in $(seq 1 $a_0); do
        eval current_vector_lenght=\$a\_$i\_0
        if [ -n "$current_vector_lenght" ]; then
            for j in $(seq 1 $current_vector_lenght); do
                eval value=\"\$a\_$i\_$j\"
                printf "$value "
            done
        fi
        printf "\n"
    done
fi

################
### MAIN END ###
################

informação relacionada