Sto cercando di utilizzare gli array nella shell Bourne (/bin/sh ). Ho scoperto che il modo per inizializzare gli elementi dell'array è:
arr=(1 2 3)
Ma sta riscontrando un errore:
syntax error at line 8: `arr=' unexpected
Ora il post in cui ho trovato questa sintassi dice che è per bash , ma non sono riuscito a trovare alcuna sintassi separata per Bourne shell. La sintassi è la stessa per /bin/sh anche tu?
Risposta accettata:
/bin/sh non è quasi mai una shell Bourne su nessun sistema al giorno d'oggi (anche Solaris, che è stato uno degli ultimi grandi sistemi a includerlo, è ora passato a un POSIX sh per il suo /bin/sh in Solaris 11). /bin/sh era la shell Thompson nei primi anni '70. La shell Bourne lo ha sostituito in Unix V7 nel 1979.
/bin/sh è stata la shell Bourne per molti anni da allora in poi (o la shell Almquist, una reimplementazione gratuita sui BSD).
Al giorno d'oggi, /bin/sh è più comunemente un interprete o un altro per POSIX sh linguaggio che è esso stesso basato su un sottoinsieme del linguaggio di ksh88 (e un superset del linguaggio della shell Bourne con alcune incompatibilità).
La shell Bourne o la specifica del linguaggio POSIX sh non supportano gli array. O meglio hanno un solo array:i parametri posizionali ($1 , $2 , [email protected] , quindi anche un array per funzione).
ksh88 aveva array che hai impostato con set -A , ma non è stato specificato in POSIX sh poiché la sintassi è scomoda e non molto utilizzabile.
Altre shell con variabili array/lists includono:csh /tcsh , rc , es , bash (che per lo più ha copiato la sintassi ksh nel modo ksh93), yash , zsh , fish ciascuno con una sintassi diversa (rc il guscio del futuro successore di Unix, fish e zsh essendo i più coerenti)…
Nello standard sh (funziona anche nelle versioni moderne della shell Bourne):
set '1st element' 2 3 # setting the array
set -- "[email protected]" more # adding elements to the end of the array
shift 2 # removing elements (here 2) from the beginning of the array
printf '<%s>n' "[email protected]" # passing all the elements of the [email protected] array
# as arguments to a command
for i do # looping over the elements of the [email protected] array ($1, $2...)
printf 'Looping over "%s"n' "$i"
done
printf '%sn' "$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 '%sn' "$# elements in the array"
printf '%sn' "$*" # 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)
(nota che nella shell Bourne e ksh88, $IFS deve contenere lo spazio per "[email protected]" per funzionare correttamente (un bug) e nella shell Bourne, non puoi accedere a elementi superiori a $9 (${10} non funzionerà, puoi comunque fare shift 1; echo "$9" o passarci sopra)).