LDP/LDP/guide/docbook/abs-guide/empty-array.sh

158 lines
4.3 KiB
Bash
Raw Permalink Normal View History

2001-07-10 14:25:50 +00:00
#!/bin/bash
# empty-array.sh
2003-05-12 14:45:05 +00:00
# Thanks to Stephane Chazelas for the original example,
2009-01-22 14:43:09 +00:00
#+ and to Michael Zick and Omair Eshkenazi, for extending it.
2008-07-20 23:16:47 +00:00
# And to Nathan Coulter for clarifications and corrections.
2003-05-12 14:45:05 +00:00
2001-07-10 14:25:50 +00:00
# An empty array is not the same as an array with empty elements.
2006-06-22 14:28:13 +00:00
array0=( first second third )
array1=( '' ) # "array1" consists of one empty element.
array2=( ) # No elements . . . "array2" is empty.
array3=( ) # What about this array?
2001-07-10 14:25:50 +00:00
2008-07-20 23:16:47 +00:00
2001-07-10 14:25:50 +00:00
echo
2003-05-12 14:45:05 +00:00
ListArray()
{
echo
2001-07-10 14:25:50 +00:00
echo "Elements in array0: ${array0[@]}"
echo "Elements in array1: ${array1[@]}"
echo "Elements in array2: ${array2[@]}"
2006-06-22 14:28:13 +00:00
echo "Elements in array3: ${array3[@]}"
2001-07-10 14:25:50 +00:00
echo
echo "Length of first element in array0 = ${#array0}"
echo "Length of first element in array1 = ${#array1}"
echo "Length of first element in array2 = ${#array2}"
2006-06-22 14:28:13 +00:00
echo "Length of first element in array3 = ${#array3}"
2001-07-10 14:25:50 +00:00
echo
echo "Number of elements in array0 = ${#array0[*]}" # 3
2004-04-28 12:08:07 +00:00
echo "Number of elements in array1 = ${#array1[*]}" # 1 (Surprise!)
2001-07-10 14:25:50 +00:00
echo "Number of elements in array2 = ${#array2[*]}" # 0
2006-06-22 14:28:13 +00:00
echo "Number of elements in array3 = ${#array3[*]}" # 0
2003-05-12 14:45:05 +00:00
}
# ===================================================================
ListArray
2004-04-28 12:08:07 +00:00
# Try extending those arrays.
2003-05-12 14:45:05 +00:00
# Adding an element to an array.
array0=( "${array0[@]}" "new1" )
array1=( "${array1[@]}" "new1" )
array2=( "${array2[@]}" "new1" )
2006-06-22 14:28:13 +00:00
array3=( "${array3[@]}" "new1" )
2003-05-12 14:45:05 +00:00
ListArray
# or
array0[${#array0[*]}]="new2"
array1[${#array1[*]}]="new2"
array2[${#array2[*]}]="new2"
2006-06-22 14:28:13 +00:00
array3[${#array3[*]}]="new2"
2003-05-12 14:45:05 +00:00
ListArray
2008-07-20 23:16:47 +00:00
# When extended as above, arrays are 'stacks' ...
# Above is the 'push' ...
2003-05-12 14:45:05 +00:00
# The stack 'height' is:
height=${#array2[@]}
echo
echo "Stack height for array2 = $height"
# The 'pop' is:
2006-06-22 14:28:13 +00:00
unset array2[${#array2[@]}-1] # Arrays are zero-based,
2004-04-28 12:08:07 +00:00
height=${#array2[@]} #+ which means first element has index 0.
2003-05-12 14:45:05 +00:00
echo
echo "POP"
echo "New stack height for array2 = $height"
ListArray
2004-04-28 12:08:07 +00:00
# List only 2nd and 3rd elements of array0.
2008-07-20 23:16:47 +00:00
from=1 # Zero-based numbering.
2006-06-22 14:28:13 +00:00
to=2
2004-02-16 17:21:02 +00:00
array3=( ${array0[@]:1:2} )
2003-05-12 14:45:05 +00:00
echo
echo "Elements in array3: ${array3[@]}"
2004-04-28 12:08:07 +00:00
# Works like a string (array of characters).
# Try some other "string" forms.
2001-07-10 14:25:50 +00:00
2004-04-28 12:08:07 +00:00
# Replacement:
2004-02-16 17:21:02 +00:00
array4=( ${array0[@]/second/2nd} )
2001-07-10 14:25:50 +00:00
echo
2003-05-12 14:45:05 +00:00
echo "Elements in array4: ${array4[@]}"
2004-04-28 12:08:07 +00:00
# Replace all matching wildcarded string.
2004-02-16 17:21:02 +00:00
array5=( ${array0[@]//new?/old} )
2003-05-12 14:45:05 +00:00
echo
echo "Elements in array5: ${array5[@]}"
2004-04-28 12:08:07 +00:00
# Just when you are getting the feel for this . . .
2004-02-16 17:21:02 +00:00
array6=( ${array0[@]#*new} )
2004-04-28 12:08:07 +00:00
echo # This one might surprise you.
2003-05-12 14:45:05 +00:00
echo "Elements in array6: ${array6[@]}"
2004-02-16 17:21:02 +00:00
array7=( ${array0[@]#new1} )
2004-04-28 12:08:07 +00:00
echo # After array6 this should not be a surprise.
2003-05-12 14:45:05 +00:00
echo "Elements in array7: ${array7[@]}"
2004-04-28 12:08:07 +00:00
# Which looks a lot like . . .
2004-02-16 17:21:02 +00:00
array8=( ${array0[@]/new1/} )
2003-05-12 14:45:05 +00:00
echo
echo "Elements in array8: ${array8[@]}"
# So what can one say about this?
# The string operations are performed on
#+ each of the elements in var[@] in succession.
2008-07-20 23:16:47 +00:00
# Therefore : Bash supports string vector operations.
# If the result is a zero length string,
2004-04-28 12:08:07 +00:00
#+ that element disappears in the resulting assignment.
2008-07-20 23:16:47 +00:00
# However, if the expansion is in quotes, the null elements remain.
2009-01-22 14:43:09 +00:00
# Michael Zick: Question, are those strings hard or soft quotes?
# Nathan Coulter: There is no such thing as "soft quotes."
#! What's really happening is that
#!+ the pattern matching happens after
#!+ all the other expansions of [word]
#!+ in cases like ${parameter#word}.
2003-05-12 14:45:05 +00:00
zap='new*'
2004-02-16 17:21:02 +00:00
array9=( ${array0[@]/$zap/} )
2003-05-12 14:45:05 +00:00
echo
2008-07-20 23:16:47 +00:00
echo "Number of elements in array9: ${#array9[@]}"
array9=( "${array0[@]/$zap/}" )
2003-05-12 14:45:05 +00:00
echo "Elements in array9: ${array9[@]}"
2008-07-20 23:16:47 +00:00
# This time the null elements remain.
echo "Number of elements in array9: ${#array9[@]}"
2003-05-12 14:45:05 +00:00
2008-07-20 23:16:47 +00:00
# Just when you thought you were still in Kansas . . .
2008-11-23 22:43:47 +00:00
array10=( ${array0[@]#$zap} )
2008-07-20 23:16:47 +00:00
echo
echo "Elements in array10: ${array10[@]}"
# But, the asterisk in zap won't be interpreted if quoted.
array10=( ${array0[@]#"$zap"} )
2003-05-12 14:45:05 +00:00
echo
echo "Elements in array10: ${array10[@]}"
2008-07-20 23:16:47 +00:00
# Well, maybe we _are_ still in Kansas . . .
# (Revisions to above code block by Nathan Coulter.)
2003-05-12 14:45:05 +00:00
2008-07-20 23:16:47 +00:00
# Compare array7 with array10.
# Compare array8 with array9.
2001-07-10 14:25:50 +00:00
2008-07-20 23:16:47 +00:00
# Reiterating: No such thing as soft quotes!
2008-11-23 22:43:47 +00:00
# Nathan Coulter explains:
2008-07-20 23:16:47 +00:00
# Pattern matching of 'word' in ${parameter#word} is done after
#+ parameter expansion and *before* quote removal.
# In the normal case, pattern matching is done *after* quote removal.
exit