2003-11-03 16:26:45 +00:00
|
|
|
#! /bin/bash
|
|
|
|
# array-append.bash
|
|
|
|
|
|
|
|
# Copyright (c) Michael S. Zick, 2003, All rights reserved.
|
|
|
|
# License: Unrestricted reuse in any form, for any purpose.
|
|
|
|
# Version: $ID$
|
|
|
|
#
|
|
|
|
# Slightly modified in formatting by M.C.
|
|
|
|
|
|
|
|
|
|
|
|
# Array operations are Bash-specific.
|
|
|
|
# Legacy UNIX /bin/sh lacks equivalents.
|
|
|
|
|
|
|
|
|
|
|
|
# Pipe the output of this script to 'more'
|
|
|
|
#+ so it doesn't scroll off the terminal.
|
2008-11-23 22:43:47 +00:00
|
|
|
# Or, redirect output to a file.
|
2003-11-03 16:26:45 +00:00
|
|
|
|
|
|
|
|
|
|
|
declare -a array1=( zero1 one1 two1 )
|
2008-11-23 22:43:47 +00:00
|
|
|
# Subscript packed.
|
2003-11-03 16:26:45 +00:00
|
|
|
declare -a array2=( [0]=zero2 [2]=two2 [3]=three2 )
|
2008-11-23 22:43:47 +00:00
|
|
|
# Subscript sparse -- [1] is not defined.
|
2003-11-03 16:26:45 +00:00
|
|
|
|
|
|
|
echo
|
|
|
|
echo '- Confirm that the array is really subscript sparse. -'
|
|
|
|
echo "Number of elements: 4" # Hard-coded for illustration.
|
2014-07-05 13:38:40 +00:00
|
|
|
for (( i = 0 ; i < 4 ; i++ ))
|
2003-11-03 16:26:45 +00:00
|
|
|
do
|
|
|
|
echo "Element [$i]: ${array2[$i]}"
|
|
|
|
done
|
|
|
|
# See also the more general code example in basics-reviewed.bash.
|
|
|
|
|
|
|
|
|
|
|
|
declare -a dest
|
|
|
|
|
|
|
|
# Combine (append) two arrays into a third array.
|
|
|
|
echo
|
|
|
|
echo 'Conditions: Unquoted, default IFS, All-Elements-Of operator'
|
|
|
|
echo '- Undefined elements not present, subscripts not maintained. -'
|
|
|
|
# # The undefined elements do not exist; they are not being dropped.
|
|
|
|
|
|
|
|
dest=( ${array1[@]} ${array2[@]} )
|
|
|
|
# dest=${array1[@]}${array2[@]} # Strange results, possibly a bug.
|
|
|
|
|
|
|
|
# Now, list the result.
|
|
|
|
echo
|
|
|
|
echo '- - Testing Array Append - -'
|
|
|
|
cnt=${#dest[@]}
|
|
|
|
|
|
|
|
echo "Number of elements: $cnt"
|
2014-07-05 13:38:40 +00:00
|
|
|
for (( i = 0 ; i < cnt ; i++ ))
|
2003-11-03 16:26:45 +00:00
|
|
|
do
|
|
|
|
echo "Element [$i]: ${dest[$i]}"
|
|
|
|
done
|
|
|
|
|
|
|
|
# Assign an array to a single array element (twice).
|
|
|
|
dest[0]=${array1[@]}
|
|
|
|
dest[1]=${array2[@]}
|
|
|
|
|
|
|
|
# List the result.
|
|
|
|
echo
|
|
|
|
echo '- - Testing modified array - -'
|
|
|
|
cnt=${#dest[@]}
|
|
|
|
|
|
|
|
echo "Number of elements: $cnt"
|
2014-07-05 13:38:40 +00:00
|
|
|
for (( i = 0 ; i < cnt ; i++ ))
|
2003-11-03 16:26:45 +00:00
|
|
|
do
|
|
|
|
echo "Element [$i]: ${dest[$i]}"
|
|
|
|
done
|
|
|
|
|
|
|
|
# Examine the modified second element.
|
|
|
|
echo
|
|
|
|
echo '- - Reassign and list second element - -'
|
|
|
|
|
|
|
|
declare -a subArray=${dest[1]}
|
|
|
|
cnt=${#subArray[@]}
|
|
|
|
|
|
|
|
echo "Number of elements: $cnt"
|
2014-07-05 13:38:40 +00:00
|
|
|
for (( i = 0 ; i < cnt ; i++ ))
|
2003-11-03 16:26:45 +00:00
|
|
|
do
|
|
|
|
echo "Element [$i]: ${subArray[$i]}"
|
|
|
|
done
|
|
|
|
|
|
|
|
# The assignment of an entire array to a single element
|
|
|
|
#+ of another array using the '=${ ... }' array assignment
|
|
|
|
#+ has converted the array being assigned into a string,
|
|
|
|
#+ with the elements separated by a space (the first character of IFS).
|
|
|
|
|
|
|
|
# If the original elements didn't contain whitespace . . .
|
|
|
|
# If the original array isn't subscript sparse . . .
|
|
|
|
# Then we could get the original array structure back again.
|
|
|
|
|
|
|
|
# Restore from the modified second element.
|
|
|
|
echo
|
|
|
|
echo '- - Listing restored element - -'
|
|
|
|
|
|
|
|
declare -a subArray=( ${dest[1]} )
|
|
|
|
cnt=${#subArray[@]}
|
|
|
|
|
|
|
|
echo "Number of elements: $cnt"
|
2014-07-05 13:38:40 +00:00
|
|
|
for (( i = 0 ; i < cnt ; i++ ))
|
2003-11-03 16:26:45 +00:00
|
|
|
do
|
|
|
|
echo "Element [$i]: ${subArray[$i]}"
|
|
|
|
done
|
|
|
|
echo '- - Do not depend on this behavior. - -'
|
|
|
|
echo '- - This behavior is subject to change - -'
|
|
|
|
echo '- - in versions of Bash newer than version 2.05b - -'
|
|
|
|
|
|
|
|
# MSZ: Sorry about any earlier confusion folks.
|
|
|
|
|
|
|
|
exit 0
|