| 1 | #! /bin/bash |
| 2 | # array-append.bash |
| 3 | |
| 4 | # Copyright (c) Michael S. Zick, 2003, All rights reserved. |
| 5 | # License: Unrestricted reuse in any form, for any purpose. |
| 6 | # Version: $ID$ |
| 7 | # |
| 8 | # Slightly modified in formatting by M.C. |
| 9 | |
| 10 | |
| 11 | # Array operations are Bash-specific. |
| 12 | # Legacy UNIX /bin/sh lacks equivalents. |
| 13 | |
| 14 | |
| 15 | # Pipe the output of this script to 'more' |
| 16 | #+ so it doesn't scroll off the terminal. |
| 17 | # Or, redirect output to a file. |
| 18 | |
| 19 | |
| 20 | declare -a array1=( zero1 one1 two1 ) |
| 21 | # Subscript packed. |
| 22 | declare -a array2=( [0]=zero2 [2]=two2 [3]=three2 ) |
| 23 | # Subscript sparse -- [1] is not defined. |
| 24 | |
| 25 | echo |
| 26 | echo '- Confirm that the array is really subscript sparse. -' |
| 27 | echo "Number of elements: 4" # Hard-coded for illustration. |
| 28 | for (( i = 0 ; i < 4 ; i++ )) |
| 29 | do |
| 30 | echo "Element [$i]: ${array2[$i]}" |
| 31 | done |
| 32 | # See also the more general code example in basics-reviewed.bash. |
| 33 | |
| 34 | |
| 35 | declare -a dest |
| 36 | |
| 37 | # Combine (append) two arrays into a third array. |
| 38 | echo |
| 39 | echo 'Conditions: Unquoted, default IFS, All-Elements-Of operator' |
| 40 | echo '- Undefined elements not present, subscripts not maintained. -' |
| 41 | # # The undefined elements do not exist; they are not being dropped. |
| 42 | |
| 43 | dest=( ${array1[@]} ${array2[@]} ) |
| 44 | # dest=${array1[@]}${array2[@]} # Strange results, possibly a bug. |
| 45 | |
| 46 | # Now, list the result. |
| 47 | echo |
| 48 | echo '- - Testing Array Append - -' |
| 49 | cnt=${#dest[@]} |
| 50 | |
| 51 | echo "Number of elements: $cnt" |
| 52 | for (( i = 0 ; i < cnt ; i++ )) |
| 53 | do |
| 54 | echo "Element [$i]: ${dest[$i]}" |
| 55 | done |
| 56 | |
| 57 | # Assign an array to a single array element (twice). |
| 58 | dest[0]=${array1[@]} |
| 59 | dest[1]=${array2[@]} |
| 60 | |
| 61 | # List the result. |
| 62 | echo |
| 63 | echo '- - Testing modified array - -' |
| 64 | cnt=${#dest[@]} |
| 65 | |
| 66 | echo "Number of elements: $cnt" |
| 67 | for (( i = 0 ; i < cnt ; i++ )) |
| 68 | do |
| 69 | echo "Element [$i]: ${dest[$i]}" |
| 70 | done |
| 71 | |
| 72 | # Examine the modified second element. |
| 73 | echo |
| 74 | echo '- - Reassign and list second element - -' |
| 75 | |
| 76 | declare -a subArray=${dest[1]} |
| 77 | cnt=${#subArray[@]} |
| 78 | |
| 79 | echo "Number of elements: $cnt" |
| 80 | for (( i = 0 ; i < cnt ; i++ )) |
| 81 | do |
| 82 | echo "Element [$i]: ${subArray[$i]}" |
| 83 | done |
| 84 | |
| 85 | # The assignment of an entire array to a single element |
| 86 | #+ of another array using the '=${ ... }' array assignment |
| 87 | #+ has converted the array being assigned into a string, |
| 88 | #+ with the elements separated by a space (the first character of IFS). |
| 89 | |
| 90 | # If the original elements didn't contain whitespace . . . |
| 91 | # If the original array isn't subscript sparse . . . |
| 92 | # Then we could get the original array structure back again. |
| 93 | |
| 94 | # Restore from the modified second element. |
| 95 | echo |
| 96 | echo '- - Listing restored element - -' |
| 97 | |
| 98 | declare -a subArray=( ${dest[1]} ) |
| 99 | cnt=${#subArray[@]} |
| 100 | |
| 101 | echo "Number of elements: $cnt" |
| 102 | for (( i = 0 ; i < cnt ; i++ )) |
| 103 | do |
| 104 | echo "Element [$i]: ${subArray[$i]}" |
| 105 | done |
| 106 | echo '- - Do not depend on this behavior. - -' |
| 107 | echo '- - This behavior is subject to change - -' |
| 108 | echo '- - in versions of Bash newer than version 2.05b - -' |
| 109 | |
| 110 | # MSZ: Sorry about any earlier confusion folks. |
| 111 | |
| 112 | exit 0 |