Created
October 25, 2020 09:54
-
-
Save TonyWhite/05c7923692a4a7b748c4745c9548cc7d to your computer and use it in GitHub Desktop.
Array - gestire elementi con spazi
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #!/bin/bash | |
| # Aggiungo elementi in un vettore: stringhe con spazi. | |
| # Ho un vettore e ci aggiungo elementi contenenti spazi. | |
| tput clear | |
| echo "Creo un array vuoto" | |
| echo "LISTA=()" | |
| LISTA=() | |
| echo "" | |
| echo "Aggiungo un paio di elementi" | |
| echo "LISTA+=(\"Primo elemento\")" | |
| echo "LISTA+=(\"Secondo elemento\")" | |
| LISTA+=("Primo elemento") | |
| LISTA+=("Secondo elemento") | |
| #Creo una funzione che mostra il contenuto dell'array | |
| function miaFunzione(){ | |
| echo "" | |
| echo "Leggo l'array" | |
| echo "TEST=(\"\$@\")" | |
| TEST=("$@") | |
| echo "" | |
| echo "Lunghezza dell'array" | |
| echo "ARR_LEN=\${#TEST[@]}" | |
| ARR_LEN=${#TEST[@]} | |
| for (( I=0; I<$ARR_LEN; I++ )); do | |
| echo "" | |
| echo "Leggo l'elemento ${I} dell'array" | |
| echo "ELEMENTO=\${TEST[\$I]}" | |
| ELEMENTO=${TEST[$I]} | |
| echo "" | |
| echo "Visualizzo il risultato" | |
| echo "echo \$ELEMENTO" | |
| echo $ELEMENTO | |
| done | |
| } | |
| echo "" | |
| echo "Passo l'array alla mia funzione" | |
| echo "miaFunzione \"\${LISTA[@]}\"" | |
| miaFunzione "${LISTA[@]}" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment