Skip to content

Instantly share code, notes, and snippets.

@TonyWhite
Created October 25, 2020 09:54
Show Gist options
  • Select an option

  • Save TonyWhite/05c7923692a4a7b748c4745c9548cc7d to your computer and use it in GitHub Desktop.

Select an option

Save TonyWhite/05c7923692a4a7b748c4745c9548cc7d to your computer and use it in GitHub Desktop.
Array - gestire elementi con spazi
#!/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