Debian Codegraph @

empty-array.sh

raw symbols SCIP
1 #!/bin/bashscip-shell filesystem . . `/bin/bash`.
2 # empty-array.sh
3
4 # Thanks to Stephane Chazelas for the original example,
5 #+ and to Michael Zick and Omair Eshkenazi, for extending it.
6 # And to Nathan Coulter for clarifications and corrections.
7
8
9 # An empty array is not the same as an array with empty elements.
10
11 array0=( first second third )
12 array1=( '' ) # "array1" consists of one empty element.
13 array2=( ) # No elements . . . "array2" is empty.
14 array3=( ) # What about this array?
15
16
17 echoscip-shell system . . echo.
18 ListArrayscip-shell shell abs-guide 10-4 ListArray().()
19 {
20 echoscip-shell system . . echo.
21 echoscip-shell system . . echo. "Elements in array0: ${array0[@]}"
22 echoscip-shell system . . echo. "Elements in array1: ${array1[@]}"
23 echoscip-shell system . . echo. "Elements in array2: ${array2[@]}"
24 echoscip-shell system . . echo. "Elements in array3: ${array3[@]}"
25 echoscip-shell system . . echo.
26 echoscip-shell system . . echo. "Length of first element in array0 = ${#array0}"
27 echoscip-shell system . . echo. "Length of first element in array1 = ${#array1}"
28 echoscip-shell system . . echo. "Length of first element in array2 = ${#array2}"
29 echoscip-shell system . . echo. "Length of first element in array3 = ${#array3}"
30 echoscip-shell system . . echo.
31 echoscip-shell system . . echo. "Number of elements in array0 = ${#array0[*]}" # 3
32 echoscip-shell system . . echo. "Number of elements in array1 = ${#array1[*]}" # 1 (Surprise!)
33 echoscip-shell system . . echo. "Number of elements in array2 = ${#array2[*]}" # 0
34 echoscip-shell system . . echo. "Number of elements in array3 = ${#array3[*]}" # 0
35 }
36
37 # ===================================================================
38
39 ListArrayscip-shell shell abs-guide 10-4 ListArray().
40
41 # Try extending those arrays.
42
43 # Adding an element to an array.
44 array0=( "${array0[@]}" "new1" )
45 array1=( "${array1[@]}" "new1" )
46 array2=( "${array2[@]}" "new1" )
47 array3=( "${array3[@]}" "new1" )
48
49 ListArrayscip-shell shell abs-guide 10-4 ListArray().
50
51 # or
52 array0[${#array0[*]}]="new2"
53 array1[${#array1[*]}]="new2"
54 array2[${#array2[*]}]="new2"
55 array3[${#array3[*]}]="new2"
56
57 ListArrayscip-shell shell abs-guide 10-4 ListArray().
58
59 # When extended as above, arrays are 'stacks' ...
60 # Above is the 'push' ...
61 # The stack 'height' is:
62 height=${#array2[@]}
63 echoscip-shell system . . echo.
64 echoscip-shell system . . echo. "Stack height for array2 = $height"
65
66 # The 'pop' is:
67 unset array2[${#array2[@]}-1] # Arrays are zero-based,
68 height=${#array2[@]} #+ which means first element has index 0.
69 echoscip-shell system . . echo.
70 echoscip-shell system . . echo. "POP"
71 echoscip-shell system . . echo. "New stack height for array2 = $height"
72
73 ListArrayscip-shell shell abs-guide 10-4 ListArray().
74
75 # List only 2nd and 3rd elements of array0.
76 from=1 # Zero-based numbering.
77 to=2
78 array3=( ${array0[@]:1:2} )
79 echoscip-shell system . . echo.
80 echoscip-shell system . . echo. "Elements in array3: ${array3[@]}"
81
82 # Works like a string (array of characters).
83 # Try some other "string" forms.
84
85 # Replacement:
86 array4=( ${array0[@]/second/2nd} )
87 echoscip-shell system . . echo.
88 echoscip-shell system . . echo. "Elements in array4: ${array4[@]}"
89
90 # Replace all matching wildcarded string.
91 array5=( ${array0[@]//new?/old} )
92 echoscip-shell system . . echo.
93 echoscip-shell system . . echo. "Elements in array5: ${array5[@]}"
94
95 # Just when you are getting the feel for this . . .
96 array6=( ${array0[@]#*new} )
97 echoscip-shell system . . echo. # This one might surprise you.
98 echoscip-shell system . . echo. "Elements in array6: ${array6[@]}"
99
100 array7=( ${array0[@]#new1} )
101 echoscip-shell system . . echo. # After array6 this should not be a surprise.
102 echoscip-shell system . . echo. "Elements in array7: ${array7[@]}"
103
104 # Which looks a lot like . . .
105 array8=( ${array0[@]/new1/} )
106 echoscip-shell system . . echo.
107 echoscip-shell system . . echo. "Elements in array8: ${array8[@]}"
108
109 # So what can one say about this?
110
111 # The string operations are performed on
112 #+ each of the elements in var[@] in succession.
113 # Therefore : Bash supports string vector operations.
114 # If the result is a zero length string,
115 #+ that element disappears in the resulting assignment.
116 # However, if the expansion is in quotes, the null elements remain.
117
118 # Michael Zick: Question, are those strings hard or soft quotes?
119 # Nathan Coulter: There is no such thing as "soft quotes."
120 #! What's really happening is that
121 #!+ the pattern matching happens after
122 #!+ all the other expansions of [word]
123 #!+ in cases like ${parameter#word}.
124
125
126 zap='new*'
127 array9=( ${array0[@]/$zap/} )
128 echoscip-shell system . . echo.
129 echoscip-shell system . . echo. "Number of elements in array9: ${#array9[@]}"
130 array9=( "${array0[@]/$zap/}" )
131 echoscip-shell system . . echo. "Elements in array9: ${array9[@]}"
132 # This time the null elements remain.
133 echoscip-shell system . . echo. "Number of elements in array9: ${#array9[@]}"
134
135
136 # Just when you thought you were still in Kansas . . .
137 array10=( ${array0[@]#$zap} )
138 echoscip-shell system . . echo.
139 echoscip-shell system . . echo. "Elements in array10: ${array10[@]}"
140 # But, the asterisk in zap won't be interpreted if quoted.
141 array10=( ${array0[@]#"$zap"} )
142 echoscip-shell system . . echo.
143 echoscip-shell system . . echo. "Elements in array10: ${array10[@]}"
144 # Well, maybe we _are_ still in Kansas . . .
145 # (Revisions to above code block by Nathan Coulter.)
146
147
148 # Compare array7 with array10.
149 # Compare array8 with array9.
150
151 # Reiterating: No such thing as soft quotes!
152 # Nathan Coulter explains:
153 # Pattern matching of 'word' in ${parameter#word} is done after
154 #+ parameter expansion and *before* quote removal.
155 # In the normal case, pattern matching is done *after* quote removal.
156
157 exit