sh - Apenas leitura matriz: nome da variável inválido

0

Meu script executa um programa e verifica a saída com um esperado. Por que não está funcionando?? (Prob um erro n00b estou fazendo)

Console:

dan9er@A-Computer:~/Projects/Project$ ./Script.sh
./Script.sh: 13: readonly: FILENAMES[0]: bad variable name

Script:

#!/bin/sh

# Copyright (c) 2017 Unattended Systems
# Some names & strings have been changed for confidentiality

#  ####### ### #       #######  #####
#  #        #  #       #       #
#  #####    #  #       #####    #####
#  #        #  #       #             #
#  #       ### ####### #######  #####

readonly FILENAMES[0]="0.file"
readonly FILENAMES[1]="1.file"
readonly FILENAMES[2]="2.file"
readonly FILENAMES[3]="3.file"
readonly FILENAMES[4]="4.file"
readonly FILENAMES[5]="5.file"
readonly FILENAMES[6]="6.file"
readonly FILENAMES[7]="7.file"
readonly FILENAMES[8]="8.file"
readonly FILENAMES[9]="9.file"
readonly FILENAMES[10]="10.file"
readonly FILENAMES[11]="11.file"
readonly FILENAMES[12]="12.file"
readonly FILENAMES[13]="13.file"
readonly FILENAMES[14]="14.file"
readonly FILENAMES[15]="15.file"
readonly FILENAMES[16]="16.file"
readonly FILENAMES[17]="17.file"
readonly FILENAMES[18]="18.file"
readonly FILENAMES[19]="19.file"
readonly FILENAMES[20]="20.file"
readonly FILENAMES[21]="21.file"

#  ####### #     # ######  #######  #####  ####### ####### ######
#  #        #   #  #     # #       #     #    #    #       #     #
#  #####      #    ######  #####   #          #    #####   #     #
#  #        #   #  #       #       #     #    #    #       #     #
#  ####### #     # #       #######  #####     #    ####### ######
#
#   #####  #     # ####### ######  #     # #######  #####
#  #     # #     #    #    #     # #     #    #    #
#  #     # #     #    #    ######  #     #    #     #####
#  #     # #     #    #    #       #     #    #          #
#   #####   #####     #    #        #####     #     #####

readonly EXPECTEDOUT[0]=$"Hello World!"
readonly EXPECTEDOUT[1]="1"
readonly EXPECTEDOUT[2]="2"
readonly EXPECTEDOUT[3]="3"
readonly EXPECTEDOUT[4]="4"
readonly EXPECTEDOUT[5]="5"
readonly EXPECTEDOUT[6]="6"
readonly EXPECTEDOUT[7]="7"
readonly EXPECTEDOUT[8]="8"
readonly EXPECTEDOUT[9]="9"
readonly EXPECTEDOUT[10]="10"
readonly EXPECTEDOUT[11]="11"
readonly EXPECTEDOUT[12]="12"
readonly EXPECTEDOUT[13]="13"
readonly EXPECTEDOUT[14]="14"
readonly EXPECTEDOUT[15]="15"
readonly EXPECTEDOUT[16]="16"
readonly EXPECTEDOUT[17]="17"
readonly EXPECTEDOUT[18]="18"
readonly EXPECTEDOUT[19]="19"
readonly EXPECTEDOUT[20]="20"
readonly EXPECTEDOUT[21]="21"

#  #        #####   #####  ###  #####
#  #       #     # #        #  #     #
#  #       #     # #  ####  #  #
#  #       #     # #     #  #  #     #
#  #######  #####   #####  ###  #####

for i in 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
do
    echo "Testing on file ${FILENAMES[$i]}"

    # Run program on file
    program ${FILENAMES[$i]} /tmp/tmp_out

    # If program's output CONTAINS the expected output for that file...
    if grep -q ${EXPECTEDOUT[$i]} /tmp/tmp_out.txt; then
        # ...record that a pass!
        echo -e "String \"${EXPECTEDOUT[$i]}\" found. Pass!"
        RESULTS[$i]=1
    else
        # ..otherwise, epic fail my dude
        echo -e "String \"${EXPECTEDOUT[$i]}\" not found. FAIL!"
        RESULTS[$i]=0
    fi
done

# Make a final list of results
PASSEDTESTS=0
for j in 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
do
    if [[ ${RESULTS[$j]} == 1 ]]; then
        echo "${FILENAMES[$j]} passed"
        PASSEDTESTS=PASSEDTESTS+1
    else
        echo "${FILENAMES[$j]} failed"
    fi
done
echo "$PASSEDTESTS/22 files passed"

Os nomes dos arquivos têm sublinhados neles.

    
por dan9er 05.10.2017 / 20:38

2 respostas

1

Como a steeldriver escreveu em um comentário ,

% bl0ck_qu0te%

Então, uma vez que você alterou o artigo para #!/bin/bash , como Waltinator sugeriu , você precisará alterar as declarações de matriz:

FILENAMES[0]="0.file"
FILENAMES[1]="1.file"
...
readonly FILENAMES

Ou você pode reduzi-los usando a atribuição de matriz ( =() ) e a sintaxe de expansão de shell ( {} ):

readonly FILENAMES=(
    {0..21}.file
    )

...

readonly EXPECTEDOUT=(
    $"Hello World!"
    {1..21}
    )

Você também pode encurtar a declaração do loop:

for i in {0..21}

...

for j in {0..21}
    
por wjandrea 05.10.2017 / 21:42
1

Ao iniciar seu script com #!/bin/sh , você está instruindo o sistema a usar o interpretador de shell dash (apenas recursos POSIX). Veja man sh;man dash . dash não suporta matrizes de shell.

Se você alterar a primeira linha para #!/bin/bash , receberá todos os recursos bash , incluindo matrizes. Veja man bash .

    
por waltinator 05.10.2017 / 20:52